Python函数参数和文档混乱

Python函数参数和文档混乱,python,Python,以下是python中的一个字符串: a = "asdf as df adsf as df asdf asd f" 假设我想用| |替换所有,那么我会: >>> a.replace(" ", "||") 'asdf||as||df||adsf||as||df||asdf||asd||f' 我的困惑来自以下信息: string.replace(s, old, new[, maxreplace]) Return a copy of string s with all o

以下是python中的一个字符串:

a = "asdf as df adsf as df asdf asd f"
假设我想用| |替换所有,那么我会:

>>> a.replace(" ", "||")
'asdf||as||df||adsf||as||df||asdf||asd||f'
我的困惑来自以下信息:

 string.replace(s, old, new[, maxreplace])
    Return a copy of string s with all occurrences...

我可以省略s,但基于我需要s的文档;然而,我只提供旧的和新的。我注意到很多python文档都是这样的;我缺少什么?

当调用对象的方法时,该对象将自动作为第一个参数提供。通常在该方法中,这被称为自我

因此,您可以调用传入对象的函数:

string.replace(s, old, new)
s.replace(old, new)
也可以调用对象的方法:

string.replace(s, old, new)
s.replace(old, new)

两者在功能上完全相同。

当调用对象的方法时,对象将自动作为第一个参数提供。通常在该方法中,这被称为自我

因此,您可以调用传入对象的函数:

string.replace(s, old, new)
s.replace(old, new)
也可以调用对象的方法:

string.replace(s, old, new)
s.replace(old, new)

两者在功能上完全相同。

方法的第一个参数是对对象的引用,该对象通常称为正在修改的self,并在使用该对象时隐式传递。方法。。。符号因此:

a = "asdf as df adsf as df asdf asd f"
print a.replace(" ", "||")
相当于:

a = "asdf as df adsf as df asdf asd f"
print str.replace(a, " ", "||")

str是对象的类。它只是语法上的糖分。

方法的第一个参数是对对象的引用,通常称为self-being-modified,并且在使用对象时隐式传递。方法。。。符号因此:

a = "asdf as df adsf as df asdf asd f"
print a.replace(" ", "||")
相当于:

a = "asdf as df adsf as df asdf asd f"
print str.replace(a, " ", "||")
str是对象的类。这只是语法上的糖分。

您将str对象方法与字符串模块函数混为一谈

您所指的文档实际上是,字符串模块中有一个名为replace的函数,它接受3个或4个参数:

In [9]: string
Out[9]: <module 'string' from '/usr/lib/python2.7/string.pyc'>

In [11]: string.replace(a, ' ', '||')
Out[11]: 'asdf||as||df||adsf||as||df||asdf||asd||f'
str对象有一个replace方法。str方法的文档是。

您将str对象方法与字符串模块函数混为一谈

您所指的文档实际上是,字符串模块中有一个名为replace的函数,它接受3个或4个参数:

In [9]: string
Out[9]: <module 'string' from '/usr/lib/python2.7/string.pyc'>

In [11]: string.replace(a, ' ', '||')
Out[11]: 'asdf||as||df||adsf||as||df||asdf||asd||f'

str对象有一个replace方法。str方法的文档是。

它是str,不是string。它是str,不是string。我要补充的是,string模块不应该使用,因为它的大部分功能都与str string类合并了+1@StefanoSanfilippo:有一些字符串函数,如string.atof,但整个模块没有被弃用。当您需要函数时,函数可能会很有用,而不是绑定到特定str的方法,常量也很有用。是的,我不久后重新编写了注释,不应使用with。我说的是字符串模块文档的第一段。对于自由函数,您可以只使用str.upper和类似的函数,例如mapstr.upper[a,b]。我要补充的是,字符串模块不应该使用,因为它的大部分功能都已与str string类合并+1@StefanoSanfilippo:有一些字符串函数,如string.atof,但整个模块没有被弃用。当您需要函数时,函数可能会很有用,而不是绑定到特定str的方法,常量也很有用。是的,我不久后重新编写了注释,不应使用with。我说的是字符串模块文档的第一段。对于自由函数,您可以只使用str.upper和类似的函数,例如mapstr.upper[a,b]。