I am a little bit confused by this example:
>>> class A:
... foo = []
>>> a, b = A(), A()
>>> a
<__main__.A instance at 0x0000000002296A88>
>>> b
<__main__.A instance at 0x0000000002296F88>
>>> a.foo.append(5)
>>> a.foo
[5]
>>> b.foo
[5]
1) How does python connect two different instances?
2) Does the instance refer to a class A()
or foo
attribute after appending the value?
But when i add __init__
method, things look different:
>>> class A:
... def __init__(self):
... self.foo = []
...
>>> a, b = A(), A()
>>> a
<__main__.A instance at 0x00000000021EC508>
>>> b
<__main__.A instance at 0x0000000002296F88>
>>> a.foo.append(5)
>>> a.foo
[5]
>>> b.foo
[]
3) What is the magic of __init__
?