I have a simple python class where I am initializing an instance variable. I am expecting that when I initialize two different objects of that class, each time the instance variable will be initialized afresh. But that is not the case I see in my notebook output. The initialization output of b is as if the object from the initialization of a is held onto.
class MyQueue(object):
def __init__(self,stackA=[]):
print(stackA)
self.stackA = stackA
stackA.append('x')
print('inside init ',stackA)
def addElement(self, elem):
print('before append ',self.stackA)
self.stackA.append(elem)
print('after append ',self.stackA)
a = MyQueue()
b = MyQueue()
a==b
I expect
[]
inside init ['x']
[]
inside init ['x']
False
But get,
[]
inside init ['x']
['x']
inside init ['x', 'x']
False