Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class variables in python not found in __dict__

Tags:

python

There is a code:

class C():
    a=1
    def f(self):
        print "f func"

a=C()
print a.a
a.f()
>>> 1
>>> f func

And when i trying to get a.__dict__ or vars(a), it shows me just {}. But

a.b=123
print a.__dict__
>>> {'b': 123}

I don't understand, why it is.

like image 685
Erop Avatar asked Oct 30 '25 23:10

Erop


1 Answers

Looking at a.__dict__ or vars(a) gives you attributes of a, which is an instance. That instance initially has no attributes of its own. The attribute a that you created in your class definition is an attribute of the class itself, not of its instances. Later when you do a.b = 123, you create an attribute just on the instance, so you can see it in the instance __dict__. You will see the attribute a if you look at C.__dict__.

When you do print a.a, Python dynamically finds the attribute a on the class C. It sees that the instance doesn't have an attribute a, so it looks on the class and finds one there. That is the value that is printed. The class attribute is not "copied" to the instance when the instance is created; rather, every individual time you try to read the instance attribute, Python checks to see if it exists on the instance, and if not it looks it up on the class.

like image 71
BrenBarn Avatar answered Nov 01 '25 13:11

BrenBarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!