__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__