Constructor does weird things with optional parameters
Mutable default arguments are a source of confusion.
See this answer: "Least Astonishment" and the Mutable Default Argument
The problem is, the default value of an optional argument is only a single instance. So for example, if you say def __init__(self, value, c=[]):
, that same list []
will be passed into the method each time an optional argument is used by calling code.
So basically you should only use immutable date types such as None
for the default value of an optional argument. For example:
def __init__(self, value, c=None):
Then you could just create a new list in the method body:
if c == None:
c = []