Home  >  Article  >  Backend Development  >  Introducing the meaning of python descriptors

Introducing the meaning of python descriptors

coldplay.xixi
coldplay.xixiforward
2020-12-21 17:48:473311browse

You may often hear the concept of "descriptor", but since most programmers rarely use it, you may not clearly understand its principle, python video The tutorial column will introduce in detail

Introducing the meaning of python descriptors

Recommended (free): python video tutorial

But if you want to advance your career and become more proficient in using python, I think you should still have a clear understanding of the concept of descriptor, which is useful for It will be of great help to your future development, and will also help you to have a deeper understanding of Python design in the future.

Although we have not directly used descriptors during the development process, they are used very frequently at the bottom level. For example, the following:

  • function, bound method, unbound method
  • Installerproperty, staticmethod, classmethod
    Are these all familiar?
    In fact, these are inextricably linked to descriptors. Let’s explore the working principles behind descriptors through the following article.

#What is a descriptor?

Before we understand what a descriptor is, we can first find an example to look at

class A:
    x = 10print(A.x) # 10

This example is very simple, let’s first look at it in class A Define a class attribute x, and then get its value.
In addition to this method of directly defining class attributes, we can also define a class attribute like this:

class Ten:
    def __get__(self, obj, objtype=None):
        return 10class A:
    x = Ten()   # 属性换成了一个类print(A.x) # 10

We can find that this time the class attribute x is not a specific Value, but a class Ten, through which Ten defines a __get__ method to return a specific value.

So it can be concluded that in python, we can host the attributes of a class to a class, and such an attribute is a descriptor
In short, Descriptor is a Binding behaviorAttribute

And what does this mean?
Recall, when we are developing, under normal circumstances, what will behavior be called? Behavior is a method.

So we can also understand the descriptor as: The attribute of the object is not a specific value, but is given to a method to define.

You can imagine, if we use a method to define an attribute, what are the benefits of doing so?

With methods, we can implement our own logic within the method. The simplest, we can assign different values ​​​​to attributes within the method according to different conditions, like the following:

class Age:
    def __get__(self, obj, objtype=None):
        if obj.name == 'zhangsan':
            return 20
        elif obj.name == 'lisi':
            return 25
        else:
            return ValueError("unknow")class Person:

    age = Age()

    def __init__(self, name):
        self.name = name

p1 = Person('zhangsan')print(p1.age)   # 20p2 = Person('lisi')print(p2.age)   # 25p3 = Person('wangwu')print(p3.age)   # unknow

In this example, the age class attribute is hosted by another class. In __get__ of this class, it will be based on the Person class attribute. name, determines what value age is.

Through such an example, we can see that through the use of descriptors, we can easily change the way a class attribute is defined.

Descriptor protocol

Understanding the definition of descriptor, now we focus on the class of managed properties.

In fact, if a class attribute wants to be hosted on a class, the methods implemented inside this class cannot be defined casually. It must comply with the "descriptor protocol", that is, the following methods must be implemented:

  • __get__(self, obj, type=None) -> value
  • __set__(self, obj, value) -> None
  • __delete__(self, obj) -> None

As long as one of the above methods is implemented, Then this class attribute can be called a descriptor.

In addition, descriptors can be divided into "data descriptors" and "non-data descriptors":

  • only defines __get___, which is called non-data In addition to defining
  • __get__
  • , descriptor also defines __set__ or __delete__, which is called data descriptor

What is the difference between them, I will elaborate below.

Now let's look at an example descriptor containing the __get__ and __set__ methods:

# coding: utf8class Age:

    def __init__(self, value=20):
        self.value = value

    def __get__(self, obj, type=None):
        print('call __get__: obj: %s type: %s' % (obj, type))
        return self.value

    def __set__(self, obj, value):
        if value <= 0:
            raise ValueError("age must be greater than 0")
        print('call __set__: obj: %s value: %s' % (obj, value))
        self.value = valueclass Person:

    age = Age()

    def __init__(self, name):
        self.name = name

p1 = Person('zhangsan')print(p1.age)# call __get__: obj: <__main__.Person object at 0x1055509e8> type: # 20print(Person.age)# call __get__: obj: None type: # 20p1.age = 25# call __set__: obj: <__main__.Person object at 0x1055509e8> value: 25print(p1.age)# call __get__: obj: <__main__.Person object at 0x1055509e8> type: # 25p1.age = -1# ValueError: age must be greater than 0

In this example, the class attribute age is a descriptor whose value depends on the Age class.

Judging from the output, when we obtain or modify the age attribute, __get__ and __set__# of Age are called. ## method:

  • 当调用 p1.age 时,__get__ 被调用,参数 objPerson 实例,typetype(Person)
  • 当调用 Person.age 时,__get__ 被调用,参数 objNonetypetype(Person)
  • 当调用 p1.age = 25时,__set__ 被调用,参数 objPerson 实例,value 是25
  • 当调用 p1.age = -1时,__set__ 没有通过校验,抛出 ValueError

其中,调用 __set__ 传入的参数,我们比较容易理解,但是对于 __get__ 方法,通过类或实例调用,传入的参数是不同的,这是为什么?

这就需要我们了解一下描述符的工作原理。

描述符的工作原理

要解释描述符的工作原理,首先我们需要先从属性的访问说起。

在开发时,不知道你有没有想过这样一个问题:通常我们写这样的代码 a.b,其背后到底发生了什么?

这里的 ab 可能存在以下情况:

  1. a 可能是一个类,也可能是一个实例,我们这里统称为对象
  2. b 可能是一个属性,也可能是一个方法,方法其实也可以看做是类的属性

其实,无论是以上哪种情况,在 Python 中,都有一个统一的调用逻辑:

  1. 先调用 __getattribute__ 尝试获得结果
  2. 如果没有结果,调用 __getattr__

用代码表示就是下面这样:

def getattr_hook(obj, name):
    try:
        return obj.__getattribute__(name)
    except AttributeError:
        if not hasattr(type(obj), '__getattr__'):
            raise    return type(obj).__getattr__(obj, name)

我们这里需要重点关注一下 __getattribute__,因为它是所有属性查找的入口,它内部实现的属性查找顺序是这样的:

  1. 要查找的属性,在类中是否是一个描述符
  2. 如果是描述符,再检查它是否是一个数据描述符
  3. 如果是数据描述符,则调用数据描述符的 __get__
  4. 如果不是数据描述符,则从 __dict__ 中查找
  5. 如果 __dict__ 中查找不到,再看它是否是一个非数据描述符
  6. 如果是非数据描述符,则调用非数据描述符的 __get__
  7. 如果也不是一个非数据描述符,则从类属性中查找
  8. 如果类中也没有这个属性,抛出 AttributeError 异常

写成代码就是下面这样:

# 获取一个对象的属性
def __getattribute__(obj, name):
    null = object()
    # 对象的类型 也就是实例的类
    objtype = type(obj)
    # 从这个类中获取指定属性
    cls_var = getattr(objtype, name, null)
    # 如果这个类实现了描述符协议
    descr_get = getattr(type(cls_var), '__get__', null)
    if descr_get is not null:
        if (hasattr(type(cls_var), '__set__')
            or hasattr(type(cls_var), '__delete__')):
            # 优先从数据描述符中获取属性            return descr_get(cls_var, obj, objtype)
    # 从实例中获取属性    if hasattr(obj, '__dict__') and name in vars(obj):
        return vars(obj)[name]
    # 从非数据描述符获取属性    if descr_get is not null:
        return descr_get(cls_var, obj, objtype)
    # 从类中获取属性    if cls_var is not null:
        return cls_var
    # 抛出 AttributeError 会触发调用 __getattr__
    raise AttributeError(name)

如果不好理解,你最好写一个程序测试一下,观察各种情况下的属性的查找顺序。

到这里我们可以看到,在一个对象中查找一个属性,都是先从 __getattribute__ 开始的。

__getattribute__ 中,它会检查这个类属性是否是一个描述符,如果是一个描述符,那么就会调用它的 __get__ 方法。但具体的调用细节和传入的参数是下面这样的:

  • 如果 a 是一个实例,调用细节为:
type(a).__dict__['b'].__get__(a, type(a))复制代码
  • 如果 a 是一个,调用细节为:
a.__dict__['b'].__get__(None, a)复制代码

所以我们就能看到上面例子输出的结果。

数据描述符和非数据描述符

了解了描述符的工作原理,我们继续来看数据描述符和非数据描述符的区别。

从定义上来看,它们的区别是:

  • 只定义了 __get___,叫做非数据描述符
  • 除了定义 __get__ 之外,还定义了 __set____delete__,叫做数据描述符

此外,我们从上面描述符调用的顺序可以看到,在对象中查找属性时,数据描述符要优先于非数据描述符调用。

在之前的例子中,我们定义了 __get____set__,所以那些类属性都是数据描述符

我们再来看一个非数据描述符的例子:

class A:

    def __init__(self):
        self.foo = 'abc'

    def foo(self):
        return 'xyz'print(A().foo)  # 输出什么?
复制代码

这段代码,我们定义了一个相同名字的属性和方法 foo,如果现在执行 A().foo,你觉得会输出什么结果?

答案是 abc

为什么打印的是实例属性 foo 的值,而不是方法 foo 呢?

这就和非数据描述符有关系了。

我们执行 dir(A.foo),观察结果:

print(dir(A.foo))# [... '__get__', '__getattribute__', ...]复制代码

看到了吗?Afoo 方法其实实现了 __get__,我们在上面的分析已经得知:只定义 __get__ 方法的对象,它其实是一个非数据描述符,也就是说,我们在类中定义的方法,其实本身就是一个非数据描述符。

所以,在一个类中,如果存在相同名字的属性和方法,按照上面所讲的 __getattribute__ 中查找属性的顺序,这个属性就会优先从实例中获取,如果实例中不存在,才会从非数据描述符中获取,所以在这里优先查找的是实例属性 foo 的值。

到这里我们可以总结一下关于描述符的相关知识点:

  • 描述符必须是一个类属性
  • __getattribute__ 是查找一个属性(方法)的入口
  • __getattribute__ 定义了一个属性(方法)的查找顺序:数据描述符、实例属性、非数据描述符、类属性
  • 如果我们重写了 __getattribute__ 方法,会阻止描述符的调用
  • 所有方法其实都是一个非数据描述符,因为它定义了 __get__

描述符的使用场景

了解了描述符的工作原理,那描述符一般用在哪些业务场景中呢?

在这里我用描述符实现了一个属性校验器,你可以参考这个例子,在类似的场景中去使用它。

首先我们定义一个校验基类 Validator,在 __set__ 方法中先调用 validate 方法校验属性是否符合要求,然后再对属性进行赋值。

class Validator:

    def __init__(self):
        self.data = {}

    def __get__(self, obj, objtype=None):
        return self.data[obj]

    def __set__(self, obj, value):
        # 校验通过后再赋值
        self.validate(value)
        self.data[obj] = value

    def validate(self, value):
        pass    
复制代码

接下来,我们定义两个校验类,继承 Validator,然后实现自己的校验逻辑。

class Number(Validator):

    def __init__(self, minvalue=None, maxvalue=None):
        super(Number, self).__init__()
        self.minvalue = minvalue
        self.maxvalue = maxvalue

    def validate(self, value):
        if not isinstance(value, (int, float)):
            raise TypeError(f'Expected {value!r} to be an int or float')
        if self.minvalue is not None and value < self.minvalue:
            raise ValueError(
                f'Expected {value!r} to be at least {self.minvalue!r}'
            )
        if self.maxvalue is not None and value > self.maxvalue:
            raise ValueError(
                f'Expected {value!r} to be no more than {self.maxvalue!r}'
            )class String(Validator):

    def __init__(self, minsize=None, maxsize=None):
        super(String, self).__init__()
        self.minsize = minsize
        self.maxsize = maxsize

    def validate(self, value):
        if not isinstance(value, str):
            raise TypeError(f'Expected {value!r} to be an str')
        if self.minsize is not None and len(value) < self.minsize:
            raise ValueError(
                f'Expected {value!r} to be no smaller than {self.minsize!r}'
            )
        if self.maxsize is not None and len(value) > self.maxsize:
            raise ValueError(
                f'Expected {value!r} to be no bigger than {self.maxsize!r}'
            )复制代码

最后,我们使用这个校验类:

class Person:

    # 定义属性的校验规则 内部用描述符实现
    name = String(minsize=3, maxsize=10)
    age = Number(minvalue=1, maxvalue=120)

    def __init__(self, name, age):
        self.name = name
        self.age = age

# 属性符合规则
p1 = Person('zhangsan', 20)print(p1.name, p1.age)# 属性不符合规则
p2 = person('a', 20)# ValueError: Expected 'a' to be no smaller than 3p3 = Person('zhangsan', -1)# ValueError: Expected -1 to be at least 1复制代码

现在,当我们对 Person 实例进行初始化时,就可以校验这些属性是否符合预定义的规则了。

function与method

我们再来看一下,在开发时经常看到的 functionunbound methodbound method 它们之间到底有什么区别?

来看下面这段代码:

class A:

    def foo(self):
        return 'xyz'print(A.__dict__['foo']) # print(A.foo)     # print(A().foo)   # >复制代码

从结果我们可以看出它们的区别:

  • function 准确来说就是一个函数,并且它实现了 __get__ 方法,因此每一个 function 都是一个非数据描述符,而在类中会把 function 放到 __dict__ 中存储
  • function 被实例调用时,它是一个 bound method
  • function 被类调用时, 它是一个 unbound method

function 是一个非数据描述符,我们之前已经讲到了。

bound methodunbound method 的区别就在于调用方的类型是什么,如果是一个实例,那么这个 function 就是一个 bound method,否则它是一个 unbound method

property/staticmethod/classmethod

我们再来看 propertystaticmethodclassmethod

这些装饰器的实现,默认是 C 来实现的。

其实,我们也可以直接利用 Python 描述符的特性来实现这些装饰器,

property 的 Python 版实现:

class property:

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        self.__doc__ = doc

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self.fget        if self.fget is None:
            raise AttributeError(), "unreadable attribute"
        return self.fget(obj)

    def __set__(self, obj, value):
        if self.fset is None:
            raise AttributeError, "can't set attribute"
        return self.fset(obj, value)

    def __delete__(self, obj):
        if self.fdel is None:
            raise AttributeError, "can't delete attribute"
        return self.fdel(obj)

    def getter(self, fget):
        return type(self)(fget, self.fset, self.fdel, self.__doc__)

    def setter(self, fset):
        return type(self)(self.fget, fset, self.fdel, self.__doc__)

    def deleter(self, fdel):
        return type(self)(self.fget, self.fset, fdel, self.__doc__)复制代码

staticmethod 的 Python 版实现:

class staticmethod:

    def __init__(self, func):
        self.func = func

    def __get__(self, obj, objtype=None):
        return self.func
复制代码

classmethod 的 Python 版实现:

class classmethod:

    def __init__(self, func):
        self.func = func

    def __get__(self, obj, klass=None):
        if klass is None:
            klass = type(obj)
        def newfunc(*args):
            return self.func(klass, *args)
        return newfunc
复制代码

除此之外,你还可以实现其他功能强大的装饰器。

由此可见,通过描述符我们可以实现强大而灵活的属性管理功能,对于一些要求属性控制比较复杂的场景,我们可以选择用描述符来实现。

总结

这篇文章我们主要讲了 Python 描述符的工作原理。

首先,我们从一个简单的例子了解到,一个类属性是可以托管给另外一个类的,这个类如果实现了描述符协议方法,那么这个类属性就是一个描述符。此外,描述符又可以分为数据描述符和非数据描述符。

之后我们又分析了获取一个属性的过程,一切的入口都在 __getattribute__ 中,这个方法定义了寻找属性的顺序,其中实例属性优先于数据描述符调用,数据描述符要优先于非数据描述符调用。

In addition, we also learned that a method is actually a non-data descriptor. If we define instance attributes and methods with the same name in the class, according to the attribute search order in __getattribute__, the instance attributes take precedence. access.

Finally we analyzed the difference between function and method, and how property and staticmethod## can also be implemented using Python descriptors. #, classmethod Decorator.

Python descriptors provide powerful attribute access control functions, which we can use in scenarios that require complex control of attributes.

This work adopts the "CC License", and the author and the link to this article must be noted when reprinting it

The above is the detailed content of Introducing the meaning of python descriptors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete