In the code below, I am expecting that a new instance of MyClass with the name 'obj' will be created every time inside the for loop. Therefore, the output should be [1] every time. But obj.mylist seems to grow. What am I missing?
class MyClass:
def __init__( self, mylist_=[] ):
self.mylist = mylist_
def addData( self ):
self.mylist.append( 1 )
for i in range(5):
obj = MyClass()
obj.addData()
print obj.mylist
The output is:
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]