0
class A:
    foo = 5
a = A()
a.foo =6

By writing a.foo = 6,

we're creating an instance attribute foo that hides a class attribute foo(source)

How do we know a.foo is the instance attribute not the class attribute? Why doesnt a use the class attribute? How to view the hidden class attribute of instance a (if possible, since it is only hidden, not erased)?

Even after reading several related answers on SO, this still puzzles me.

Second question:

print id(A.foo)
A.foo = 0
print id(A.foo)

Why does the id of A.foo change? It is the same class attribute pointing to different integers.

Community
  • 1
  • 1
J.Joe
  • 644
  • 2
  • 7
  • 20
  • your second question could be in a standalone question (maybe has been answered?) and can be reduced to a simpler case: `a = 3`, `id(a)`, `a=5`, `id(a)`. – MariusSiuram May 05 '16 at 10:23
  • And, to provide a partial answer "How to view the hidden class attribute of instance `a`?" The attribute is not for instance `a`, is an attribute for _class_ `A` so you can view it through `A.foo`. – MariusSiuram May 05 '16 at 10:28

0 Answers0