Difference between _ get __,__ getattr __,__ getattribute _ in python3, python3 _ get __

Source: Internet
Author: User
Tags access properties

Difference between _ get __,__ getattr __,__ getattribute _ in python3, python3 _ get __

_ Get __,__ getattr _ and _ getattribute are both methods for accessing attributes, but they are not the same.
Object. _ getattr _ (self, name)
If the attribute cannot be found in a common location, getattr is called to return a value or AttributeError.

Object. _ getattribute _ (self, name)
It is called unconditionally and accessed through an instance. If _ getattr _ () is defined in the class, _ getattr _ () is not called unless it is displayed as a call or an AttributeError exception is thrown)

Object. _ get _ (self, instance, owner)
If a class is defined, this class can be called a descriptor. The owner is the owner's class, And the instance is the instance that accesses the descriptor. If it is accessed not through the instance but through the class, the instance is None. (When a descriptor instance accesses itself, it will not trigger _ get __, but will trigger _ call __. only the descriptor as an attribute of other classes makes sense .) (So the following d is called as an attribute of C2)

class C(object):    a = 'abc'    def __getattribute__(self, *args, **kwargs):        print("__getattribute__() is called")        return object.__getattribute__(self, *args, **kwargs)#        return "haha"    def __getattr__(self, name):        print("__getattr__() is called ")        return name + " from getattr"        def __get__(self, instance, owner):        print("__get__() is called", instance, owner)        return self        def foo(self, x):        print(x)class C2(object):    d = C()if __name__ == '__main__':    c = C()    c2 = C2()    print(c.a)    print(c.zzzzzzzz)    c2.d    print(c2.d.a)

 

The output result is:

__getattribute__() is calledabc__getattribute__() is called__getattr__() is called zzzzzzzz from getattr__get__() is called <__main__.C2 object at 0x16d2310> <class '__main__.C2'>__get__() is called <__main__.C2 object at 0x16d2310> <class '__main__.C2'>__getattribute__() is calledabc

 

Conclusion: we can see thatInstanceAll access properties pass through the _ getattribute _ function. If the attribute does not exist, you still need to access _ getattribute __, but then access _ getattr __. This is like an exception handling function.
Every time you access descriptor (that is, the class that implements _ get _), it passes through the _ get _ function first.

Note that when you use a class to access a non-existent variable, it does not pass through the _ getattr _ function. The descriptor does not have this problem, but identifies the instance as none.

From: http://luozhaoyu.iteye.com/blog/1506426

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.