Home > Backend Development > Python Tutorial > Detailed introduction to descriptors in Python

Detailed introduction to descriptors in Python

巴扎黑
Release: 2017-09-16 10:02:15
Original
1400 people have browsed it

This article will share the knowledge about descriptors in Python. It is very good and has reference value. Friends who need it can refer to it

Definition

Usually, a descriptor is an object property with "binding behavior". The bound behavior can be overridden by custom __get__() , __set__() and __delete__() methods through the descriptor protocol. If any of the above three methods of an object is overridden, it can be called a descriptor.

The default operations for properties are to get, set, and delete a property from the object dictionary. For example, if a. If an object in the search chain has a descriptor method defined, Python will override the default behavior.

Descriptor is a powerful tool. Although developers do not often come into contact with it, it is actually the operating mechanism behind classes, properties, functions, methods, static methods, class methods and super().

Descriptor protocol

The three method prototypes are as follows:


descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None
Copy after login

The data descriptor has both_ Objects with _get__() and __set__() methods, if they only have __get__() methods, are non-data descriptors. If there is an entry with the same name as the data descriptor in the instance dictionary, the data descriptor has higher priority. In contrast, non-data descriptors have low priority.

Let the __set__() method throw an exception to create a read-only data descriptor.

Call descriptor

descriptor can be called directly through the method name. For example, d.__get__(obj) .

It is more common to automatically call the descriptor by accessing the object properties. For example, if d defines the method __get__(), then obj.d calls d.__get__(obj).

For objects, b.x will be converted to type(b).__dict__['x'].__get__(b, type(b)) . And for classes (yes, classes can be called too), B.x will be converted to B.__dict__['x'].__get__(None, B) .

Descriptor Example


class RevealAccess(object):
  """A data descriptor that sets and returns values
    normally and prints a message logging their access.
  """
  def __init__(self, initval=None, name='var'):
    self.val = initval
    self.name = name
  def __get__(self, obj, objtype):
    print('Retrieving', self.name)
    return self.val
  def __set__(self, obj, val):
    print('Updating', self.name)
    self.val = val
>>> class MyClass(object):
...   x = RevealAccess(10, 'var "x"')
...   y = 5
...
>>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5
Copy after login

The above is the detailed content of Detailed introduction to descriptors in Python. For more information, please follow other related articles on the PHP Chinese website!

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