有人可以解释staticmethod的源代码在python中如何工作 [英] Can someone explain how the source code of staticmethod works in python

查看:120
本文介绍了有人可以解释staticmethod的源代码在python中如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我了解装饰器的总体工作方式。而且我知道 @staticmethod 去除了签名中的实例参数,使

First of all, I understand how, in general, a decorator work. And I know @staticmethod strips off the instance argument in the signature, making

class C(object):
    @staticmethod
    def foo():
        print 'foo'
C.foo    //<function foo at 0x10efd4050>
C().foo  //<function foo at 0x10efd4050>

有效。

但是,我不理解 staticmethod 的sourcec代码如何实现这一目标。

However, I don't understand how the sourcec code of staticmethod make this happen.

在我看来,将方法 foo 包装在 staticmethod 中时,是一个实例的静态方法实例化,然后发生一些魔术,使 C.foo()合法

It seems to me that when wrapping method foo in staticmethod, an instance of staticmethod is instantiated, then some magic happens, making C.foo() legit.

那么..那些魔术中会发生什么? 静态方法做什么?

So.. what happen in those magic? what did staticmethod do?

我知道关于静态方法的大量主题,但没有一个能解决我的疑问。但是也许我没有打魔术关键字。如果是这样,请告诉我。

I'm aware the enormous topics on SO regarding staticmethods but none of them addresses my doubts. But maybe I didn't hit the magic keyword. If so, please kindly let me know.

对于寻找 staticmethod 源代码的人,请参考 https://hg.python.org/cpython/file/c6880edaf6f3/Objects/funcobject.c

For whoever looking for staticmethod source code, please refer to https://hg.python.org/cpython/file/c6880edaf6f3/Objects/funcobject.c

推荐答案

静态方法对象是 描述符 。您缺少的魔术是,当在类或实例上将对象作为属性访问对象时,Python调用 __ get __ 方法。

因此,以 C.foo 的形式访问对象会导致Python将其转换为 C .__ dict __ ['foo'] .__ get __(无,C ),而 instance_of_C.foo 变为 type(instace_of_C).__ dict __ ['foo'] .__ get __(instance_of_C,type (instance_of_C))

So accessing the object as C.foo results in Python translating that to C.__dict__['foo'].__get__(None, C), while instance_of_C.foo becomes type(instace_of_C).__dict__['foo'].__get__(instance_of_C, type(instance_of_C)).

静态方法对象为在C代码中定义,但Python中的等效项是:

The staticmethod object is defined in C code, but an equivalent in Python would be:

class staticmethod(object):
    def __init__(self, callable):
        self.f = callable
    def __get__(self, obj, type=None):
        return self.f
    @property
    def __func__(self):
        return self.f

其中 self.f 是原始包装

所有这些都是必需的,因为函数本身也是描述符。它是为您提供方法对象的描述符协议(请参阅 python绑定和未绑定的方法对象了解更多详情)。由于它们也具有 __ get __ 方法,而没有 staticmethod 对象包装函数,因此 functionobj .__ get __ 调用生成一个方法对象,传入一个 self 自变量。

All this is needed because functions are themselves descriptors too; it is the descriptor protocol that gives you method objects (see python bound and unbound method object for more details). Since they too have a __get__ method, without a staticmethod object wrapping the function, a functionobj.__get__ call produces a method object instead, passing in a self argument.

还有一个 classmethod ,它使用 descriptor .__ get __ 的第二个参数将函数绑定到该类,并且然后有 property 对象,它们将绑定直接转换为函数调用。参见 @property装饰器如何工作?

There is also a classmethod, which uses the second argument to descriptor.__get__ to bind a function to the class, and then there are property objects, which translate binding into a function call directly. See How does the @property decorator work?.

这篇关于有人可以解释staticmethod的源代码在python中如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆