Search strategies for __get__ vs __getattr__ vs __getattribute__ and properties in Python

Source: Internet
Author: User

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:
    1. Called if the __getattribute__ is overloaded.
    2. A.__dict__, the instance is not allowed to have descriptor, so do not encounter descriptor
    3. A.__dict__, that is a.__class__.__dict__. If descriptor is encountered, descriptor is called first.
    4. 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

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.