string.replace 的正确形式是什么? [英] What is the correct form for string.replace?
问题描述
遵循 string.replace 的 Python 文档(http://docs.python.org/library/string.html ):
<块引用>string.replace(str, old, new[, maxreplace])
返回字符串 str 的副本,其中所有出现的子字符串 old 都被 new 替换.如果给出了可选参数 maxreplace,则替换出现的第一个 maxreplace.
使用给定的格式会产生以下错误:
<预><代码>>>>a = '感激'>>>a.replace(a,'t','c')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.类型错误:需要一个整数您需要str"似乎很奇怪重复并从错误中我猜到我的第三个参数被用于 maxreplace.
格式:
<块引用>string.replace(old, new)
似乎按预期运行.
我想知道我是否误解了什么,Python 文档中给出的形式实际上在某种程度上是正确的.
我认为您在这里的困惑(以及大多数答案的困惑)是 string
模块和 之间的不同str
内置类.它们是完全独立的东西,即使在功能上有很多重叠.
string.replace(s, old, new)
是一个免费的函数,而不是一个方法.您无法将其称为 s.replace(old, new)
,因为 s
不能是 string
模块的实例.
str.replace(self, old, new)
是一种方法.与任何其他方法(除了 classmethod 和 staticmethod 方法)一样,您可以(通常会这样做)通过 str
实例调用它,如 s.replace(old, new)
,其中 s
自动成为 self
参数.
你也可以通过类调用一个方法,所以str.replace(s, old, new)
结果和s.replace(old, new)完全一样代码>.碰巧的是,如果
s
是一个str
,它的作用与string.replace(old, new)
完全相同.但由于历史原因,这确实是一个巧合.
作为旁注,您几乎从不想在 string
模块中调用函数.它们主要是 Python 早期版本的遗留物.事实上,string.replace
列在文档的已弃用的字符串函数"部分下,您可能会在那里寻找的大多数其他函数也是如此.整个模块没有被弃用的原因是它有一些不属于 str
(或 bytes
或 unicode
) 类,例如像 string.digits
这样的常量.
Following the Python documentation for string.replace ( http://docs.python.org/library/string.html ):
string.replace(str, old, new[, maxreplace])
Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
Using the format as given generates the following error:
>>> a = 'grateful'
>>> a.replace(a,'t','c')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
It seems odd that you'd need the "str" repeated and from the error I guessed that my third argument was being taken for maxreplace.
The format:
string.replace(old, new)
does seem to function as expected.
I'm wondering if I am misunderstanding something, and the form given in the Python documentation is, in fact, correct in some way.
I think your confusion here (and that of most of the answers) is the different between the string
module and the str
built-in class. They're entirely separate things, even if there is a lot of overlap in functionality.
string.replace(s, old, new)
is a free function, not a method. There's no way you can call it as s.replace(old, new)
, because s
cannot be an instance of the string
module.
str.replace(self, old, new)
is a method. As with any other method (other than classmethod and staticmethod methods), you can—and usually do—call it through a str
instance, as s.replace(old, new)
, where s
becomes the self
parameter automatically.
You can also call a method through the class, so str.replace(s, old, new)
turns out to be exactly the same as s.replace(old, new)
. And it just so happens that, if s
is a str
, this does the exact same thing as string.replace(old, new)
. But that's really a coincidence that's true for historical reasons.
As a side note, you almost never want to call functions in the string
module. They're mostly a holdover from very early versions of Python. In fact, string.replace
is listed under the "Deprecated string functions" section in the documentation, as are most of the other functions you'd probably go looking for there. The reason the whole module hasn't been deprecated is that it has some things that don't belong in the str
(or bytes
or unicode
) class, such as constants like string.digits
.
这篇关于string.replace 的正确形式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!