What does python descriptor do?

(*-*)浩
Release: 2020-09-18 12:08:43
Original
4944 people have browsed it

The role of the python descriptor: Proxy the attributes of a class, allowing programmers to customize the work to be done when referencing an object attribute; it is the implementation of the lowest-level data structure in most Python class features Method is a very important component in large frameworks that use decorators or metaclasses.

What does python descriptor do?

#This article mainly introduces the definition of descriptors and some personal understandings. I hope that after reading this article, you have a clearer understanding of descriptors.

What is a descriptor

Official definition: A descriptor is an object attribute with "binding behavior". When accessing (getting, setting, and deleting) its properties, special methods (_get_(), _set_(), _delete_()) are actually called. That is, if an object defines any of these three methods, it is a descriptor.

The function of the descriptor is to proxy the attributes of a class. It should be noted that the descriptor cannot be defined in the constructor of the used class. It can only be defined as an attribute of the class. It only belongs to the class. does not belong to an instance, we can confirm this by looking at the dictionary of instances and classes.

Descriptors are the means to implement the lowest-level data structures in most Python class features. We often use attributes such as @classmethod, @staticmethd, @property, and even __slots__. This is achieved through descriptors. It is one of the important tools for many high-level libraries and frameworks, and is a very important component in large frameworks that use decorators or metaclasses. Note: Concepts such as decorators and metaclasses will be explained in future articles.

The following is an example of a descriptor and the code that refers to the descriptor class:

class Descriptors:
 
    def __init__(self, key, value_type):
        self.key = key
        self.value_type = value_type
 
    def __get__(self, instance, owner):
        print("执行Descriptors的get")
        return instance.__dict__[self.key]
 
    def __set__(self, instance, value):
        print("执行Descriptors的set")
        if not isinstance(value, self.value_type):
            raise TypeError("参数%s必须为%s"%(self.key, self.value_type))
        instance.__dict__[self.key] = value 
 
    def __delete__(self, instance):
        print("执行Descriptors的delete")
        instance.__dict__.pop(self.key) 
 
class Person:
 
    name = Descriptors("name", str)
    age = Descriptors("age", int)
 
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
 
person = Person("xiaoming", 15)
print(person.__dict__)
person.name
person.name = "jone"
print(person.__dict__)
Copy after login

Among them, the Descriptors class is a descriptor, and Person is the class that uses the descriptor.

The __dict__ attribute of a class is a built-in attribute of the class. The static functions, class functions, ordinary functions, global variables and some built-in attributes of the class are placed in the class __dict__.

When outputting the descriptor variable, the __get__ method in the descriptor will be called. When setting the descriptor variable, the __set__ method in the descriptor will be called.

The results of the above example are as follows:

What does python descriptor do?

The above is the detailed content of What does python descriptor do?. 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