[Python-ideas] in str.replace(old, new), allow 'old' to accept a tuple
cs at zip.com.au
Thu Apr 12 05:58:36 CEST 2012
On 12Apr2012 14:47, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
| Ben Finney wrote:
| > Tshepang Lekhonkhobe <tshepang at gmail.com>
| > writes:
| >>>>>'foo bar baz'.replace(('foo', 'bar'), 'baz')
| > You can't replace multiple matches “at the same time”, as you're
| > implying.
| An obvious thing to do is to try them in the order they
| appear in the sequence. That would argue against allowing
| an unordered collection.
And likewise with Ben's set() suggestion.
I for one would allow it.
If the order matters, the caller can produce a sequence with the
required order. If the order doesn't matter (you know no replacement
overlaps, and no replacement introduces text that itself should get
replaced), then why not allow a set?
I vote for any iterable if this ges ahead. The specification should sy
that replacements happen in the order items come from the iterable,
leaving the choice of control up to the caller but providing predicable
behaviour if the caller provides a predictable sequence.
| Not quite so obvious is whether the replacements should
| be considered as candidates for further replacements.
| I would say not, because it complicates the algorithm
| and in my experience is rarely needed.
Not to mention recursion!
| If you want that,
| you would just have to do multiple replace calls like
| you do now.
| And how about allowing a sequence of (old, new) pairs
| instead of just a single replacement? That would be even
| more useful.
Sure. But doesn't that break the function signature? I suppose we're
already there though.
Do you want to special case the single string replacement or
require callers to use zip(repls, [ "foo" for s in repls ])?
Personally, I would require the zip; the, um, flexibility of the
%-format operator with string-vs-list has long bothered me to the point
%of always providing a sequence, even a single element tuple.
Cameron Simpson <cs at zip.com.au> DoD#743
Mountain rescue teams insist the all climbers wear helmets, and fall haedfirst.
They are then impacted into a small globular mass easily stowed in a rucsac.
- Tom Patey, who didnt, and wasnt
More information about the Python-ideas