Built-in attribute __getattribute__ in Python

Reference from: https://blog.csdn.net/yitiaodashu/article/details/78974596 

__getattribute__ is an attribute access interceptor
, that is, when an attribute of this class is accessed, it will automatically call the __getattribute__ method of the class in

Python as long as If the class that inherits the object is defined, there is an attribute interceptor by default, but it does not perform any operation after intercepting, but returns directly. So we can rewrite our own __getattribute__methods to implement related functions, such as viewing permissions, printing log logs, etc.

 

important point:

Prevent looping calls in built-in properties:
for example:
class Tree(object):
    def __init__(self,name):
        self.name = name
        self.cate = " plant " 
    def  __getattribute__ (self,obj):
         if obj.endswith( " e " ):
             return object. __getattribute__ (self,obj)
         else :
             return self.call_wind()
     def call_wind(self):
         return  " The tree attracts the wind " 
aa = Tree( " big tree " )
 print (aa.name) #Because the name ends with e, the returned name is still the name, so print out the "big tree" 
print (aa.wind)

When running to aa.wind, it will crash because of the loop call

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325839283&siteId=291194637