Python“请参阅help(type(self))以获取准确的签名”。 [英] Python "See help(type(self)) for accurate signature."

查看:209
本文介绍了Python“请参阅help(type(self))以获取准确的签名”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

help()设置类时,我在许多文档字符串中看到以下语句:请参见 help(type(self)) 以获得准确的签名。

I have seen the following statement in a number of docstrings when help()ing a class: "See help(type(self)) for accurate signature."

值得注意的是,它在 help() scipy.stats.binom .__ init __ 和至少 stockfish.Stockfish .__ init __ 。因此,我认为这是某种股票信息。

Notably, it is in the help() for scipy.stats.binom.__init__ and for stockfish.Stockfish.__init__ at the very least. I assume, therefore, that it is some sort of stock message.

无论如何,我不知道这到底意味着什么。这是有用的信息吗?请注意,可以说,作为班级的外部,我永远无法访问自己。此外,如果我无法访问 __ init __ 方法的签名,因此无法执行 help(type(my_object_instantiated ))。这是一个难处。22.为了使用 __ init __ ,我需要 __ init __ 的签名,但是要阅读 __ init __ 的签名,我需要使用 __ init __ 实例化一个对象。这一点严格来说是学术性的,即使我确实实例化了 scipy.stats.binom ,它实际上返回的对象完全是 >类, rv_frozen ,在其 __ init __ 文档字符串中具有完全相同的消息,但其签名完全不同且完全更少有用。换句话说, help(type(self))实际上没有给出准确的签名。没用。

In any case, I can't figure out what the heck it means. Is this useful information? Note that, being "outside" of the class, so to speak, I never have access to self. Furthermore, it is impossible to instantiate a class if I cannot access the signature of the __init__ method, and can therefore not even do help(type(my_object_instantiated)). Its a catch 22. In order to use __init__, I need the signature for __init__, but in order to read the signature for __init__, I need to instantiate an object with __init__. This point is strictly academic however, for even when I do manage to instantiate a scipy.stats.binom, it actually returns an object of an entirely different class, rv_frozen, with the exact same message in its __init__ docstring, but whose signature is entirely different and entirely less useful. In other words, help(type(self)) actually does not give an accurate signature. It is useless.

有人知道此消息来自何处,或者我应该怎么做?

Does anyone know where this message comes from, or what I'm supposed to make of it? Is it just stock rubbish from a documentation generator, or am I user-erroring?

推荐答案

有一个约定,即签名用于构造一个类实例放在 class 上的 __ doc __ 中(因为这就是用户的调用),而不是放在上__init __ (或 __ new __ )确定该签名。对于扩展类型(用C编写)尤其如此,这些扩展类型的 __ init __ 不能通过自省发现其签名。

There is a convention that the signature for constructing a class instance is put in the __doc__ on the class (since that is what the user calls) rather than on __init__ (or __new__) which determines that signature. This is especially true for extension types (written in C) whose __init__ cannot have its signature discovered via introspection.

您看到的消息是 type 类的一部分(请参见 help(type .__ init __)),因此被继承

The message that you see is part of the type class (see help(type.__init__)) and is thus inherited by metaclasses by default.

在某些版本中, scipy.stats.binom 由于实际上不是一个类型;它只是可以调用的另一个类的实例(如 type )。因此,在其上寻求 help 只会为那个类提供帮助(就像 help(1)为您提供 help(int))—您必须查看其 __ call __ 以获得更多信息(如果有) )。并要求在调用的结果上提供 help ,如您所观察到的,它为您返回的实际类提供了帮助。

In some versions, scipy.stats.binom confuses the matter by not actually being a type; it is merely an instance of another class that (like type) is callable. So asking for help on it merely gives the help for that class (just like help(1) gets you help(int))—you have to look at its __call__ for further information (if any). And asking for help on the result of calling it gives you help for the actual class of whatever it returns, as you observed.

这篇关于Python“请参阅help(type(self))以获取准确的签名”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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