[Tutor] Baffling problem with a list of objects sharing a property
Danny Yoo
dyoo at hashcollision.org
Thu May 26 14:25:07 EDT 2016
> They are. You've stiumbled on one of those Python peculiarities of
> implementation that can be useful and annoying in equal measure.
>
>> class Test(object):
>> def __init__(self, name, paths=[]):
>> self.name = name
>> self.paths = paths
>
> When you give a function/method a default value that same object is used
> for *every* invocation of the method where the default applies.
By the way, this is one of the things that tools like pylint
(https://www.pylint.org/) will warn about.
http://pylint-messages.wikidot.com/messages:w0102
Just to note that there are external "lint" tools that can help catch
this class of problems automatically.
Best of wishes!
More information about the Tutor
mailing list