
On Sun, 22 Mar 2020 at 14:01, Dennis Sweeney <sweeney.dennis650@gmail.com> wrote:
Is there a proven use case for anything other than the empty string as the replacement? I prefer your "replacewhatever" to another "stripwhatever" name, and I think it's clear and nicely fits the behavior you proposed. But should we allow a naming convenience to dictate that the behavior should be generalized to a use case we're not sure exists, where the same same argument is passed 99% of the time?
I think so, as if we don't, then we'd end up with the following three methods on str objects (using Guido's suggested names of "removeprefix" and "removesuffix", as I genuinely like those): * replace() * removeprefix() * removesuffix() And the following questions still end up with relatively non-obvious answers: Q: How do I do a replace, but only at the start or end of the string? A: Use "new_prefix + s.removeprefix(old_prefix)" or "s.removesuffix(old_suffix) + new_suffix" Q: How do I remove a substring from anywhere in a string, rather than just from the start or end? A: Use "s.replace(substr, '')" Most of that objection would go away if the PEP added a plain old "remove()" method in addition to removeprefix() and removesuffix(), though - the "replace the substring with an empty string" trick isn't the most obvious spelling in the world, whereas I'd expect a lot folks to reach for "s.remove(substr)" based on the regular sequence API, and I think Guido's right that in many cases where a prefix or suffix is being changed, you also want to add it if the old prefix/suffix is missing (and in the cases where you don't then, then you can either use startswith()/endswith() first, or else check for a length change.
I think a downside would be that a pass-a-string-or-a-tuple-of-strings interface would be more mental effort to keep track of than a ``*args`` variadic interface for "(cut/remove/without/trim)prefix", even if the former is how ``startswith()`` works.
I doubt we'd use *args for any new string methods, precisely because we don't use it for any of the existing ones. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia