Difference:
__getattribute__: is called unconditionally. The __getattribute__ method is called implicitly when the property of any object is visited, such as calling T.__dict__, which actually runs the t.__ getattribute__ ("__dict__") function. So assuming that we are in the overload __getattribute__ and call __dict__, will be infinite recursion, with the object of the great God to avoid, that is Object.__getattribute __ (self, name).
__getattr__: __getattr__ is called only when __getattribute__ cannot be found.
__get__: It's descriptor.
If we have a Class A, where a is an instance of a
What happened when a.x?
The lookup order of the properties is as follows:
- If __getattribute__ is overloaded, it is called.
- A.__dict__, the instance is not agreeable to have descriptor, so do not encounter descriptor
- A.__dict__, also known as a.__class__.__dict__. If you encounter a descriptor, call descriptor first.
- Searches the parent class along the inheritance chain. Search all __dict__ in a.__class__.__bases__. If you have multiple inheritance and a diamond inheritance, search by MRO (Method Resolution order).
If none of the above is found, then the Attributeerror exception is thrown.
PS. can see from above, Dot (.) Operations are expensive, very many implicit calls, with particular emphasis on performance, and in high-frequency loops, the ability to consider binding to a temporary local variable.
Here's a code snippet for everyone to do their own exploration.
Class C (object): def __setattr__ (self, Name, value): print "__setattr__ called:", name, value object.__ Setattr__ (self, name, value) def __getattr__ (self, name): print "__getattr__ called:", name def __ getattribute__ (self, name): print "__getattribute__ called:", name return object.__getattribute__ (self, name c = C () c.x = "foo" Print c.__dict__[' x ']print c.x
Elegant Python-__get__ vs __getattr__ vs __getattribute__ and properties search strategy