小编zum*_*aum的帖子

静态方法如何不绑定到“ staticmethod”类?

我试图了解描述符如何在python中工作。我了解的很大,但是在理解@staticmethod装饰器时遇到了问题。

我具体指的代码来自相应的python文档:https : //docs.python.org/3/howto/descriptor.html

class Function(object):
    . . .
    def __get__(self, obj, objtype=None):
        "Simulate func_descr_get() in Objects/funcobject.c"
        if obj is None:
            return self
        return types.MethodType(self, obj)
Run Code Online (Sandbox Code Playgroud)
class StaticMethod(object):
    "Emulate PyStaticMethod_Type() in Objects/funcobject.c"

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

    def __get__(self, obj, objtype=None):
        return self.f
Run Code Online (Sandbox Code Playgroud)

我的问题是:self.f在最后一行中访问when时,它本身不会f被识别为描述符(因为每个函数都是非数据描述符),因此绑定到self,这是一个StaticMethod对象吗?

python python-decorators python-descriptors

5
推荐指数
1
解决办法
43
查看次数