2016-12-25 40 views
2

help(help)这是围绕pydoc.help(扭曲)的包装。在Python中调用帮助(帮助)时,“带有扭曲”是什么意思?

什么是扭曲?

$ 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) 

回答

6

没有太大的转折。转折是,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。引用臭虫记者:

“(一个扭曲)”非常感谢。我认为评论应该被删除或解释。参考手册应该解释,而不是戏弄。

其次是开发者的回应:

同意。我认为这个“扭曲”是导入是懒惰的,而且帮助有一个有用的repr(哇,谈论一个转折!)。这些细节不需要暗示,只是删除评论(“包装”是国际海事组织足以提高好奇心,来源是在这里找到什么是包装)。

文档字符串现在读:

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'. 
    """