I was working on my project and encountered this strange behavior. The following code shows how to reproduce it.
class TestClass:
def __init__(self, arr=[]):
self.arr = arr
if __name__ == '__main__':
for i in range(3):
test = TestClass()
test.arr.append('x')
print(test.arr)
I would expect the following output:
['x']
['x']
['x']
since new TestClass instance is created inside the loop. But instead, I got:
['x']
['x', 'x']
['x', 'x', 'x']
Did I misunderstand something?
python --version output:
Python 3.6.3 :: Anaconda, Inc.