[Python-ideas] Complicate str methods
leewangzhong+python at gmail.com
Sat Feb 3 17:04:48 EST 2018
Let s be a str. I propose to allow these existing str methods to take
params in new forms.
Allow passing in a collection of olds.
Allow passing in a single argument, a mapping of olds to news.
Allow the olds in the mapping to be tuples of strings.
s.split(sep), s.rsplit, s.partition:
Allow sep to be a collection of separators.
Allow argument to be a collection of strings.
s.find, s.index, s.count, x in s:
These methods are also in `list`, which can't distinguish between
items, subsequences, and subsets. However, `str` is already inconsistent
with `list` here: list.M looks for an item, while str.M looks for a
Sadly, these functions already interpret their str arguments as
collections of characters.
These new forms can be optimized internally, as a search for multiple
candidate substrings can be more efficient than searching for one at a
The most significant change is on .replace. The others are simple enough to
simulate with a loop or something. It is harder to make multiple
simultaneous replacements using one .replace at a time, because previous
replacements can form new things that look like replaceables. The easiest
Python solution is to use regex or install some package, which uses (if
you're lucky) regex or (if unlucky) doesn't simulate simultaneous
replacements. (If possible, just use str.translate.)
I suppose .split on multiple separators is also annoying to simulate. The
two-argument form of .split may be even more of a burden, though I don't
know when a limited multiple-separator split is useful. The current best
solution is, like before, to use regex, or install a package and hope for
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas