静态方法对象描述的含义? [英] Meaning of staticmethod object's description?

查看:55
本文介绍了静态方法对象描述的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实践中理解 @staticmethod 装饰器.但是模拟静态方法的错误使我陷入了 Python 语义兔子洞.标准类型层次结构部分中的此描述让我很困惑:

I understand the @staticmethod decorator in practice. But a bug in mocking a static method led me down the Python semantics rabbit hole. This description in The standard type hierarchy section is confusing me:

静态方法对象提供了一种击败转换的方法上面描述的函数对象到方法对象.静态方法对象是任何其他对象的包装器,通常是用户定义的方法对象.检索静态方法对象时从一个类或一个类实例,实际返回的对象是包裹的对象,不受任何进一步转换的影响.静态方法对象本身不可调用,尽管他们包装的对象通常是.静态方法对象由内置的 staticmethod() 构造函数.

Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in staticmethod() constructor.

staticmethod()构造函数将函数对象作为唯一参数.它如何包装除函数对象之外的任何其他对象?即使这没有失败,这又有什么意义呢?

The staticmethod() constructor takes a function object as sole argument. How can it wrap any other object than a function object? Even if this doesn't fail, how does it make any sense?

它通常如何包装用户定义的方法对象而不是函数对象?用户定义的方法对象在调用时,将调用它们的对象添加到参数列表的开头,然后调用存储在类中的函数对象(忽略所有各种特殊情况).

How is it usually a wrapper around a user-defined method object instead of a function object? User-defined method objects, when called, add the object they're called on to the start of the argument list, then call the function object stored on the class (ignoring all the various special cases).

静态方法对象本身是如何不可调用的?那么,如何调用这些工作呢?

How is it that static method objects are not themselves callable? How do calls to these work, then?

推荐答案

它如何包装函数对象以外的任何其他对象?

How can it wrap any other object than a function object?

很容易.

class Example(object):
    example = staticmethod(5)

print(Example.example) # prints 5

你可以向staticmethod构造函数传递任何你想要的东西.

You can pass anything you want to the staticmethod constructor.

即使这没有失败,那又有什么意义呢?

Even if this doesn't fail, how does it make any sense?

通常不会,但 staticmethod 不会检查.

It usually doesn't, but staticmethod doesn't check.

它通常如何包装用户定义的方法对象而不是函数对象?

How is it usually a wrapper around a user-defined method object instead of a function object?

不是.那部分是错误的.

It's not. That part's just wrong.

静态方法对象本身是如何不可调用的?那么,如何调用这些函数?

How is it that static method objects are not themselves callable? How do calls to these work, then?

描述符协议.静态方法对象有一个 __get__ 方法,该方法返回它们包装的任何对象.属性访问调用此 __get__ 方法并返回 __get__ 返回的内容.

The descriptor protocol. Static method objects have a __get__ method that returns whatever object they wrap. Attribute access invokes this __get__ method and returns what __get__ returns.

这篇关于静态方法对象描述的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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