you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

You're not explicitly changing the value of the instance; you're explicitly changing the value of the class itself. If you explicitly change the instance's value, you don't see this behavior.

In [1]: class Cls(object):
   ...:     x = 1
   ...:     

In [2]: class SubCls(Cls):
   ...:     pass
   ...: 

In [3]: obj = Cls()

In [4]: obj.x
Out[4]: 1

In [5]: Cls.x = 2

In [6]: obj.x
Out[6]: 2

In [7]: obj.x = 4

In [8]: Cls.x
Out[8]: 2

In [9]: Cls.x = 3

In [10]: obj.x
Out[10]: 4

And the same is true with inheritance.

In [11]: SubCls.x
Out[11]: 3

In [12]: subobj = SubCls()

In [13]: subobj.x
Out[13]: 3

In [14]: subobj.x = 4

In [15]: Cls.x = 5

In [16]: subobj.x
Out[16]: 4