I have a question regarding new instances in Python. The following code as a minimal example
class A(object):
def __new__(cls, *p, **k):
inst = object.__new__(cls)
return inst
def __init__(self, params=[]):
self.params = params
def add_param(self, p):
self.params.append(p)
a = A()
print a.params
a.add_param(1)
a.add_param(2)
print a.params
b = A()
print b.params
With output
[]
[1, 3]
[1, 3]
From my basic understanding I would have suspected, that b
would also have an empty parameter list. However, b
is created with the parameters added to a
.
So, my question is: Why is that and how can I get to instantiate b without any parameters (apart from explicitly providing params=[] as an argument).