Elegant Python-__get__ vs __getattr__ vs __getattribute__ and properties search strategy

Source: Internet
Author: User

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:

    1. If __getattribute__ is overloaded, it is called.
    2. A.__dict__, the instance is not agreeable to have descriptor, so do not encounter descriptor
    3. A.__dict__, also known as a.__class__.__dict__. If you encounter a descriptor, call descriptor first.
    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.