As mentioned before, Python has no real private properties. This then leads to poor encapsulation of Python classes. We sometimes want Python to be able to define private properties and then provide publicly accessible get methods and set methods. Python can actually achieve encapsulation through magic methods.
Method | Description |
__getattr__(self, name) | This method definition Changes the behavior when you try to access a property that doesn't exist. Therefore, overloading this method can catch misspellings and redirect them, or warn about some deprecated attributes. |
__setattr__(self, name, value) | defines the behavior when assigning and modifying attributes. Regardless of whether an attribute of the object exists, assignment to the attribute is allowed. One thing to note is that when implementing __setattr__, you must avoid the error of "infinite recursion", |
__delattr__(self , name) | __delattr__ is very similar to __setattr__, except that it defines the behavior when you delete an attribute. Implementing __delattr__ is to avoid "infinite recursion" errors at the same time |
__getattribute__(self, name) | __getattribute__ defines the behavior when your attributes are accessed. Comparison, __getattr__ only works if the attribute does not exist. Therefore, in Python versions that support __getattribute__, __getattribute__``__getattribute__ must be called before calling __getattr__ to avoid "infinite recursion" errors. |
As you can see from the above method table, you may easily cause an error when defining attribute access control. You can take a look at the following example:
def __setattr__(self, name, value): self.name = value # 每当属性被赋值的时候, ``__setattr__()`` 会被调用,这样就造成了递归调用。 # 这意味这会调用 ``self.__setattr__('name', value)`` ,每次方法会调用自己。这样会造成程序崩溃。 def __setattr__(self, name, value): # 给类中的属性名分配值 self.__dict__[name] = value # 定制特有属性
The specific example of calling the above method is as follows:
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- class User(object): def __getattr__(self, name): print('调用了 __getattr__ 方法') return super(User, self).__getattr__(name) def __setattr__(self, name, value): print('调用了 __setattr__ 方法') return super(User, self).__setattr__(name, value) def __delattr__(self, name): print('调用了 __delattr__ 方法') return super(User, self).__delattr__(name) def __getattribute__(self, name): print('调用了 __getattribute__ 方法') return super(User, self).__getattribute__(name) if __name__ == '__main__': user = User() # 设置属性值,会调用 __setattr__ user.attr1 = True # 属性存在,只有__getattribute__调用 user.attr1 try: # 属性不存在, 先调用__getattribute__, 后调用__getattr__ user.attr2 except AttributeError: pass # __delattr__调用 del user.attr1
The output result:
调用了 __setattr__ 方法 调用了 __getattribute__ 方法 调用了 __getattribute__ 方法 调用了 __getattr__ 方法 调用了 __delattr__ 方法Next Section