I'm facing strange issue with my code bellow. I simple cannot understand, why the simpleFiled
attribute of myClass
remain "prepoluated" from previous "round".
Code:
class myClass:
simpleField = []
name = ''
def __init__(self,simple_field=[]):
self.simpleField = simple_field
print('In __init__ %s ' % str(simple_field))
def __str__(self):
return self.name
def __repr__(self):
return self.name+' '.join(str(self.simpleField))
def fill(name='default'):
m = myClass()
m.name = name
for i in range(0,5):
m.simpleField.append(i)
return m
if __name__ == '__main__':
for i in range(0,2):
m = fill('round '+str(i))
print(m.__repr__())
Output (I numbered lines for better understanding)
1. In __init__ []
2. round 0[ 0 , 1 , 2 , 3 , 4 ]
3. In __init__ [0, 1, 2, 3, 4]
4. round 1[ 0 , 1 , 2 , 3 , 4 , 0 , 1 , 2 , 3 , 4 ]
Lines 1 and 2 are pretty obvious and no magic over here. I cannot understand why in line 3 I have received such a simple_field
values. As I can see in my code I create the instance of the class without any arguments - m = myClass()
Thank you for any advice
Y