描述符修饰符的确切称呼是什么?

问题描述 投票:-1回答:1

给出以下模拟静态方法的代码:

class StaticMethod(object):
    "Emulate PyStaticMethod_Type() in Objects/funcobject.c"
    def __init__(self, f):
        self.f = f
    def __get__(self, obj, objtype=None):
        print('getting')
        return self.f

class A:
    def func2():
        print('hello')

    func2 = StaticMethod(func2)

当我打电话时:A.func2我得到了我所期望的:

getting
<function __main__.A.func2>

[当我打电话时:A.func2()我得到:

getting
hello

这是否意味着当您每次调用Descriptor Decorator方法时,Python首先会从Descriptor的__get__方法中检索它?

如果是,那么实际上如何调用该方法?到底发生了什么?

python static-methods python-decorators python-descriptors
1个回答
0
投票

这是否意味着当您每次调用Dedecor Decorator方法时,Python首先会从Descriptor的get方法中检索它?

当访问其类的类属性为描述符的对象的属性时,该对象将调用__get__并返回结果。

“描述符装饰器”并不是真正的东西,使用装饰器设置描述符的事实与它的功能无关。

如果是,那么实际上如何调用该方法?到底发生了什么?

[如果您的意思是用staticmethod装饰的基础函数,则每次调用该方法时都会调用该方法,描述符/ staticmethod本身并没有规定,它只是返回该函数。

© www.soinside.com 2019 - 2024. All rights reserved.