Python descriptors are a way to create managed properties. Whenever an attribute is queried, an action occurs. This action defaults to get, set or delete. However, sometimes an application may have more requirements and require you to design more complex actions. The best solution is to write a function that performs the desired action, and then specify that it runs when the property is accessed. An object
with this functionality is called a descriptor. Descriptors are the basis for the implementation of Python methods, bound methods, super, PRperty, staticmethod and classmethod.
1. Descriptor protocol
A descriptor is an object that represents an attribute value. By implementing one or more __get__, __set__, __delete__ methods, the descriptor can be linked to the attribute access mechanism, and these can also be customized. operate.
__get__(self, instance, own): used to access attributes and return the value of the attribute. Instance is the instance object using the descriptor, and own is the class to which the instance belongs. When accessing a property through a class, instance is None.
__set__(self,instance,value): Set attribute value.
__delete__(self,instance): Delete attribute value.
2. How to implement descriptors
class Descriptor(object):
def __get__(self, instance, owner): print 'getting:%s'%self._name
return self._name
def __set__(self, Instance, name):
Print' setting:%s'%name
self._name = name
def __delete __ (self, instance):
Print 'deleting:%s'%seld._name
SS Person (object):
name = Descriptor()
A very simple descriptor object is generated. Now you can read, set and delete the attribute name of a Person object:
>>> p.name='john'
setting:john>>> p.name
getting:john
'john'
>>> del p. name
deleting:john
Note: Descriptors can only be instantiated at the class level, descriptors cannot be created for each instance by creating descriptor objects in __init__() and other methods.
Class binding: If A is a class object, call the method: A.__dict__['attr'].__get__(None,A)Super binding: If a is a super instance, then super(B,obj).m () Find B’s base class A by querying obj.__class__.__mro__, and then execute A.__dict__['m'].__get__(obj,obj.__class__)3. Descriptor that performs attribute type checking
class TypedProperty(object):
def __init__(self,name,attr_type,default=None):
self.name='_'+name
self.type=attr_type
self.default=default if default else attr_type()
def __get__(self,instance,own):
return getattr(instance,self.name,self.default)
def __set__(self,instance,value):
if not isinstance(value,self.type):
raise TypeError,'Must be %s'%self.type
setattr(instance,self.name,value) def __delete__(self,instance):
raise AttributeError('Can not delete attribute')
class Foo(object):
name=TypedProperty('name',str)
num=TypedProperty('num',int,37)
TypeError: Must be
AttributeError: Can not delete attribute