Where the __getattribute__ is unconditionally invoked.
The __getattribute__ method is implicitly called when a property is accessed for any object, such as calling T.__dict__, which actually executes the t.__getattribute__ ("__dict__") function. So if we're overloaded __ getattribute__ call __dict__ words, will be infinite recursion, with the object of the great God to avoid, that is, object.__getattribute__ (self, name).
The __getattr__ is called only when __getattribute__ cannot find it.
and __get__ is descriptor.
Let's say 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:
- Called if the __getattribute__ is overloaded.
- A.__dict__, the instance is not allowed to have descriptor, so do not encounter descriptor
- A.__dict__, that is a.__class__.__dict__. If descriptor is encountered, descriptor is called first.
- Searches the parent class along the inheritance chain. Search all __dict__ in a.__class__.__bases__. In case of multiple inheritance and diamond inheritance, search by MRO (Method Resolution order).
If the above are not found, then throw Attributeerror exception.
PS. From the above can be seen, dot (.) Operations are expensive, many implicit calls, with particular emphasis on performance, in high-frequency loops, you can 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
Search strategies for __get__ vs __getattr__ vs __getattribute__ and properties in Python