Interesting facts about Python instance attributes and class attributes.

Python instances can read class attributes, but class attributes are not instance attributes.

Today I tested the method of reading attributes of Python's __getattribute__ instance .

It's really interesting. When an instance reads a class variable, it still reads it through __getattribute__ .

But when reading methods, it does not take __getattribute__ to read, in fact, those methods in the class are basically class attributes

It's just that the function object is special, has a __get__ method, and belongs to a non-data descriptor.

This way of understanding, it feels right to go down, and the data of the property @property will not take the __getattribute__ method.

In this way, you can understand __getattribute__, when the instance reads non-descriptive attributes, whether it is an instance or a class attribute.

As long as the attribute exists, it is read via __getattribute__ .

 

Guess you like

Origin www.cnblogs.com/sidianok/p/12677427.html