python - 在Python中调用help(help)时 'with a twist'是什么意思?

标签 python python-2.x pydoc

help(help)This is a wrapper around pydoc.help (with a twist).

什么是扭曲?

$ python 
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(help)
Help on _Helper in module site object:

class _Helper(__builtin__.object)
 |  Define the builtin 'help'.
 |  This is a wrapper around pydoc.help (with a twist).
 |  
 |  Methods defined here:
 |  
 |  __call__(self, *args, **kwds)
 |  
 |  __repr__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

最佳答案

没有太大的转折。不同之处在于 pydoc惰性导入的,并且该对象有一个 __repr__ 方法,该方法提供有关如何使用该对象的直接反馈:

>>> repr(help)
'Type help() for interactive help, or help(object) for help about object.'

每当您在交互式提示中回显 help 对象时,就会调用 __repr__ 方法:

>>> help
Type help() for interactive help, or help(object) for help about object.

Python 3.4 完全摆脱了“扭曲”的描述,取而代之的是更具描述性的东西;见issue 9364 .引用漏洞报告者的话:

"(with a twist)" thanks a lot. I think the comment should be either removed or explained. A reference manual should explain, not tease.

随后是开发者的回应:

Agreed. I think the “twist” is that the import is lazy and that help has a useful repr (wow, talk about a twist!). Those details need not be alluded to, just remove the comment (“wrapper” is IMO enough to raise curiosity, the source is here to find what’s the wrapping).

文档字符串现在是这样的:

class _Helper(object):
    """Define the builtin 'help'.

    This is a wrapper around pydoc.help that provides a helpful message
    when 'help' is typed at the Python interactive prompt.

    Calling help() at the Python prompt starts an interactive help session.
    Calling help(thing) prints help for the python object 'thing'.
    """

关于python - 在Python中调用help(help)时 'with a twist'是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322555/

相关文章:

python - 阅读第 3 方模块的 Python 文档

python - f-strings 应该在 Python 3.4 中工作吗?

python - 插件架构 - 插件管理器与从插件导入中检查 *

python - Pandas:带有两个条形图和两个 y 轴的条形图

python - pretty-print JSON 转储

Python Wiki 样式文档生成器

python - 如何展平大循环堆栈?

Python 用当前时间戳重命名相对路径

python - 简单的 Python 温度转换器

python - iPython 中的 Pydocs 类似于 bpython