Steven D'Aprano added the comment:
This is not a bug, this is working as the language is designed, and the
behaviour occurs for all functions, not just class methods. Default values are
only evaluated once, when the function is defined, not every time the function
is called.
This is even de
William Pickard 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 a
New submission from Ashish Shevale :
Consider the following piece of code
class MyClass:
def do_something(self, a, b = []):
b.append(a)
print("b contains", b)
def caller(self):
a = (1,2)
self.do_something(a)
a = MyClass().caller()
a = MyClass().caller()