Python descriptor and Attribute lookup order

Source: Internet
Author: User

1.1(ref:http://hbprotoss.github.io/posts/python-descriptor.html) first introduced the next __dict__

Example:

class A (object):     = 1    def__init__(self, a_l):        = a_l    def  foo (self):         Pass

Print a.__dict__

A = a (5)
Print a.__dict__

The output is as follows:

{'__module__':'__main__',   'A_g': 1,    '__dict__': <attribute'__dict__'Of'A'Objects>,    'Foo': <function foo at 0x10bdfb668>,   '__weakref__': <attribute'__weakref__'Of'A'Objects>,   '__doc__': None,'__init__': <function__init__At 0x10bdfb050>}{'_a_l': 5}

As you can see, a contains all the methods and the global variables of the class (these methods can also be considered global),

A (5) contains only the variables passed in at the time of initialization (__init__ self.xx that type of attribute)

The 1.2 instance does not contain a method of the class, but when we call A.foo (), we can find the correct method, which is why?

>> A.foo

<bound method A.foo of <__main__. A Object at 0x1094504d0>>

>> a.__dict__[' foo ']

<function Foo at 0x10944d668>

>>> a.__dict__[' foo '].__get__ (A, a)
<bound method A.foo of <__main__. A Object at 0x1094504d0>>

When the Foo attribute is obtained from a, it is followed in a certain order. This depends on which class of Foo is the object.

The concept of descriptor is introduced here:

A descriptor is actually an object of a particular class.

If this class defines both the __get__ and __set__ methods, then the object of this class is called data descriptor.

If this class only defines the __get__ method, then the object of this class is called Non-data descriptor

If a descriptor a_d as a property of Class A, then when object A of a accesses A_d, A_d's __get__ is called, __set__ method

In fact, a.__dict__[' foo ' is actually a descriptor, and when we a.foo, we actually call the __get__ method of this descriptor.

1.3 (ref:http://www.cnblogs.com/xybaby/p/6270551.html)

The order of instance property lookups is:

obj = Clz (), then the obj.attr (equivalent to obj.attr) order is as follows:

(1) If "attr" appears in the __dict__ of CLZ or its base class, and attr is data descriptor, call its __get__ method, otherwise

(2) If "attr" appears in the __dict__ of obj, then return directly to obj.__dict__[' attr '), otherwise

(3) If "attr" appears in the __dict__ of CLZ or its base class

(3.1) If attr is Non-data descriptor, then call its __get__ method, otherwise

(3.2) return __dict__[' attr ']

(4) If CLZ has a __getattribute__ method, call the __getattribute__ method, and if the attributeerror is thrown, continue calling

__getattr__. Otherwise throws Attributeerror end.

Python descriptor and Attribute lookup order

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.