What is the correct form for string.replace?

Following the Python documentation for string.replace ( http://docs.python.org/library/string.html ):

string.replace (str, old, new [, maxreplace])

Returns a copy of the string str with all occurrences of the substring replaced by the new one. If the optional maxreplace argument is specified, the first instances of maxreplace will be replaced.

Using the specified format 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 strange that you will need "str", and from the error I guessed that my third argument was made for maxreplace.

Format:

string.replace (old, new)

works as expected.

I am wondering if I misunderstand something, and the form given in the Python documentation is actually correct in some way.

+4
source share
4 answers

I think your confusion here (and most of the answers) is different from the string module and the str built-in class. These are completely different things, even if there are many overlaps in functionality.

string.replace(s, old, new) is a free function, not a method. You cannot call it like s.replace(old, new) , because s cannot be an instance of the string module.

str.replace(self, old, new) - method. Like any other method (except for the classmethod and staticmethod methods), you can and usually execute it through a str instance, like s.replace(old, new) , where s automatically becomes a self parameter.

You can also call the method through the class, so str.replace(s, old, new) turns out to be exactly the same as s.replace(old, new) . And it so happened that if s is str , it does the same as string.replace(old, new) . But this is indeed a coincidence, true for historical reasons.

As a note, you almost never want to call functions in the string module. They are mostly a delay from very early versions of Python. In fact, string.replace is listed in the "Deprecated String Functions" section of the documentation, like most other functions that you probably looked for there. The reason the whole module is not deprecated is because it has some things that do not belong to the str class (either bytes or unicode ), for example, constants like string.digits .

+7
source

Yes, this document is correct as it refers to the use of string.replace() as a standalone function. So you can do this:

 >>> import string >>> string.replace("a","a","b") 'b' 

This is different from calling replace() as a method of a given string, for example:

 >>> 'a'.replace('a','b') 'b' 

These are two different things that have different syntax, but are designed to produce the same results. Therefore, calling one with the other syntax will result in an error. For instance:

 >>> 'a'.replace('a','a','b') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: an integer is required 
+3
source

It looks like you are confusing the string module's replace method with the python string's replace method.

 string.replace("rest,"r", "t") 

will return a "test"

 "rest".replace("r", "t") 

will return a "test"

 "rest".replace("rest", "r", "t") 

will return the specified error

+2
source

Python methods use an explicit self. That is, where C ++ / javscript has this magic variable, Python passes it explicitly as the first argument. When you call a method, the object to the left of the point becomes the first argument to the method.

They are equivalent:

 str.replace('foobar', 'o', 'O', 1) 'foobar'.replace('o', 'O', 1) 

You see that in both cases there are only four values ​​(and one class: str ).

0
source

Source: https://habr.com/ru/post/1435128/


All Articles