The following happens:
When you instantiate a new object f1 = Foo()
, it has no attributes of its own. Whenever you try to access, for example, f1.a
, you are redirected to the Foo.a
classes:
print f1.__dict__ {} print f1.a 1
However, if you set f1.a = 5
, the instance will receive a new attribute of this value:
print f1.__dict__ {'a': 5}
The class definition is not affected by this, like any other instances.
In the second example, you are not reassigning anything. With append
you only use the very list that was defined in the class. Thus, your instance still refers to this list, like all other instances.
source share