4 ms·
>- using a mutable object as default value or in a class attribute Python noob here. Can you expand on that a little? Or perhaps link a post? I can't think of
by misterbowfinger 9y ago
>- using a mutable object as default value or in a class attribute
Python noob here. Can you expand on that a little? Or perhaps link a post? I can't think of an example or how it'd impact memory
- hamandcheese 9y agoIt would only lead to a memory leak depending on your usage, but it’s pretty common gotcha that can be annoying with or without memory issues. The following is some pretty good reading on the matter, and the example given is also one that would lead to a memory leak: https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument https://stackoverflow.com/questions/1132941/least-astonishme...
- nitely 9y agoHere[0]. Mutable objects as default values create a single instance of the object. For iterables (list, set, etc) the behaviour is particularly surprising coz it accumulates/memoize every item ever added (i.e a list that grows/leaks with every call to the function). If you use a good IDE (like pycharm), it will warn you about this stuff. [0] http://docs.python-guide.org/en/latest/writing/gotchas/ http://docs.python-guide.org/en/latest/writing/gotchas/