I wrote the following python code.
I was expecting a new tmp
instance in every loop, so every time I print tmp.c
, I should get "[1]"
.
Why is this happening?
class f():
c = []
def __init__(self):
self.c.append(1)
for i in range(5):
tmp = f()
print(tmp.c)
print(tmp)
The output is:
<__main__.f object at 0x7f7566b0b7f0>
[1, 1]
<__main__.f object at 0x7f7566b0b668>
[1, 1, 1]
<__main__.f object at 0x7f7566b0b828>
[1, 1, 1, 1]
<__main__.f object at 0x7f7566b0b668>
[1, 1, 1, 1, 1]
<__main__.f object at 0x7f7566b0b828>