William Pickard <[email protected]> added the comment:
This is not a bug but a side-affect at how defaulted parameters are stored. The
rule of thumb is to never use mutable values as default values for parameters.
When a method is created in the Python runtime, it checks if the signature has
defaulted keyword arguments. If it does, it executes the expression to retrieve
the value for the arguments and stores the results internally.
When you go and execute the method with these arguments missing, Python
retrieves a reference the the generated value and provides your method with
that. This is your issue as you're modifying the same object with every call to
the method.
The proper way to do this is this:
def do_something(self, a, b=None):
b = b if b is not None else []
b.append(a)
print('b contains', b)
----------
nosy: +WildCard65
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44216>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com