[Python-ideas] string.replace should accept a list as a first argument

Andrew Barnert abarnert at yahoo.com
Tue Oct 6 22:59:59 CEST 2015


On Oct 6, 2015, at 12:25, Emil Rosendahl Petersen <emilrosendahlpetersen at outlook.com> wrote:
> 
> I think string.replace should be changed accept a list as a first
> argument.

Just a list? If I call it with a tuple, or some other kind of iterable, should I get a TypeError telling me to pass a str or list?

But of course str is also an iterable of str, which makes it ambiguous if you do otherwise. Python does have a few functions they can take either one Foo or multiple Food (and Foo may itself be iterable), but everywhere else it uses tuple as the special type, not list.

> That way, if I had this string:
> 
> "There are a lot of undesirable people in this filthy world"
> 
> Then I could do this, replace(['undesirable', 'filthy'], ''), in case
> that's what I wanted to do.

Even without the multiple arguments, this is a very strange use of replace. For one thing, it leaves extra spaces behind. Also, I suspect your be tempted to use it similarly if you wanted to remove "filth", and the complain that it turned "filthy" into "y".

I suspect that, in your actual motivating code, you want to either split the string, filter it, and rejoin it, or use regular expressions or a more complicated parser.

> Now, string.replace doesn't accept a list as its first argument, and
> complains about implicit conversion.
> 
> Is there any great obstacle to just having the function loop over that
> list, calling itself in case we get a list argument instead of a str?

There are various inconsistent things that replace could do with multiple arguments, but I think this is the one you'd be least likely to want when they differ, and that would surprise people the most.

Consider cases like removing "fily" and "th": should removing the "th" from "filthy" make it eligible to have the remaining "fily" replaced? Does it matter which order they're passed in the list? What if the replacement string isn't "" but "c"; does that mean "filcy" can be replaced? Of course as long as you think of a string as a sequence of words rather than a sequence of characters, you don't think of these issues—which is exactly why I think you should probably be splitting the string into words, so you don't have to.

Or, if you really do want to do this character by character, you need to think through what you mean by how it affects order, greediness, etc. Often, the simplest way to express that is a regular expression, in which case, just do that. If you explicitly want something that's hard to express in a regexp, it's probably uncommon enough that you don't want it as a method on str, and want to have the logic in clear Python code for later reading.

> Doesn't that seem like the more obvious behaviour? To me the results of
> running the above code should be unsurprising, if this change was
> implemented: "there are a lot of people in this world".
> 
> / Emil Petersen
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list