在字符串实例上使用 help() 显示"No Python documentation found for 'x'"



我有一个类似的程序

>>> str1 = 'Python'
>>> help(str1)
No Python documentation found for 'Python'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

即使我用其他字符串修改这个str1,我也会得到相同的输出

>>> str1 = 'programming'    #another string value
>>> help(str1)
No Python documentation found for 'programming'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
>>> tuple1 = 'hai'    # using another variable
>>> help(tuple1)
No Python documentation found for 'hai'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

但如果我这样使用它:

>>> str1=''
>>> help(str1)
Help on class str in module builtins:
class str(object)
 |  str(object='') -> str
...

还有一件事我注意到了:

>>> str1='list'
>>> help(str1)
Help on class list in module builtins:
class list(object)
 |  list(iterable=(), /)
...

当我将字符串作为输入时,为什么help()的作用与其他类型不同?

来自help()文档:

如果参数是字符串,则会将该字符串作为名称查找模块、函数、类、方法、关键字或文档主题,并且在控制台上打印帮助页面。如果有任何争论其他类型的对象,则会生成该对象的帮助页面。

因此,当您使用非空字符串时,它将被视为名称(报价的第一部分(:

>>> help('list') == help(list) == help([])
Help on class list in module builtins:
class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
[...]
True

但是,当您传递字符串时,它将被视为一个对象,您将获得该类型的文档(引用的第二部分(:

>>> help('') == help(str)
Help on class str in module builtins:
class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
[...]
True

来自文档:

如果没有给出任何参数,交互式帮助系统将在解释器控制台。如果参数是字符串,则字符串为查找为模块、函数、类、方法、关键字的名称,或文档主题,并在控制台上打印帮助页面。如果参数是任何其他类型的对象,对象上的帮助页是生成。

当您调用help(something)时,该something必须是对象、模块、函数、类、方法或文档主题,这样它才能打印其文档字符串(用于描述如何在python中使用内容的特殊变量(。否则,这就是无稽之谈。

最新更新