当我打字时help('string')
在 python 解释器中,我获得有关字符串类的信息。那里,upper()
被表示为函数。但我只能将其称为类似的方法"hi".upper()
代替upper("hi")
.
因此,我们可以假设任何方法都将在内置模块的文档字符串中指示为函数。然而当我这样做时help('list')
,列表类的方法在文档字符串中表示为方法!
为什么会这样呢?仅仅是因为写医书的人不一致或者不同人写的吗?或者这些方法(文档字符串中称为“函数”的方法与称为“方法”的方法)实际上具有不同的属性吗?
当你搜索时help('string')
,您正在寻找的文档字符串string
module。如果你这样做help(str)
or help('str')
你会得到的文档字符串str
类型,然后在这里upper
出现为一种方法!
正如你在这里看到的,该函数upper
来自string
模块实际上是一个函数而不是方法:
>>> upper('hi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'upper' is not defined
>>> 'hi'.upper() # method from the str type
'HI'
>>> from string import upper
>>> upper('hi') # function from the string module
'HI'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)