Python-Class Property Query protocol-__getattr__ __getattribute__

Source: Internet
Author: User

__getattr__

Called when a class attribute is not found

Class Beimenchuixue:    def __init__ (self, name):        self.name = name    def __getattr__ (self, item):        print ("% s not find ", item)        Raise Exception (" instance method not found ") if __name__ = = ' __main__ ':    Bei_men_chui_xue = Beimenchuixue (" North Gate blowing Snow ")    print (bei_men_chui_xue.name)    # without this property    print (Bei_men_chui_xue.hello)

  

__getattribute__

Unconditional entry, whether the lookup attribute exists, can not be rewritten without rewriting

Class Beimenchuixue:    def __init__ (self, name):        self.name = name    def __getattribute__ (self, item):        Print ("%s  find"% item) If __name__ = = ' __main__ ':    Bei_men_chui_xue = Beimenchuixue ("North Gate Blowing Snow")    # can find, will find actually _ _getattribute__ return value    print (bei_men_chui_xue.name)    # does not have this property    print (Bei_men_chui_xue.hello)

  

Python-Class Property Query protocol-__getattr__ __getattribute__

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.