Home > Database > Mysql Tutorial > body text

python descriptor descriptor(1)

黄舟
Release: 2016-12-23 17:37:18
Original
1141 people have browsed it

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 =Person()

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

Property names used by classes with descriptors have higher priority than property names stored on instances. In order for a descriptor to store a value on an instance, the descriptor must pick a name that is different from the name it itself uses.

As in the above example, the name cannot be used when the Person class initialization __init__ function sets attributes for the instance.

data descriptor and none-data descriptor:

If __get__ and __set__ are implemented, it is a data descriptor, if only __get__ is implemented, it is a non-data descriptor. The different effect is that the data descriptor always replaces the attribute implementation in an instance,

and the non-data descriptor does not have a set, when assigning values ​​​​to attributes through the instance, such as the above p.name = 'hello', it does not The __set__ method will be called again, and the instance attribute p.name will be directly set to 'hello'.

Of course, if you only raise AttributeError in __set__, you will still get a non-data descriptor.

Descriptor calling mechanism:

When querying the attribute a.attr of an object, if python finds that attr is a descriptor object, how to read the attribute depends on the object a:

Direct call: The simplest call is to use it directly The code calls the descriptor method, attr.__get__(a)

Instance binding: If a is an instance object, call the method: type(a).__dict__['attr'].__get__(a,type(a))

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)


The above descriptor can check the type of the attribute. If the name attribute is not set to str type or num is not If set to int type, an error will be reported:

>>> f.name=21

TypeError: Must be

and deletion of attributes is prohibited:

> >> del f.name

AttributeError: Can not delete attribute

f.name Invisible call type(f).__dict__['name'].__get__(f,Foo), that is, Foo.name.__get__( f,Foo).

The above descriptor is actually stored on the instance. Name is stored on f._name through setattr(f,_name,value), and num is stored on f._num. This is also the reason for underlining,

Otherwise the descriptor The name name will conflict with the instance attribute name, and the descriptor attribute f.name will overwrite the instance attribute f.name.

The above is the content of python descriptor descriptor (1). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!