[Python-ideas] in str.replace(old, new), allow 'old' to accept a tuple

Cameron Simpson cs at zip.com.au
Thu Apr 12 06:01:02 CEST 2012

On 12Apr2012 11:47, Ben Finney <ben+python at benfinney.id.au> wrote:
| Tshepang Lekhonkhobe <tshepang at gmail.com>
| writes:
| > >>> 'foo bar baz'.replace(('foo', 'bar'), 'baz')
| > baz baz baz
| How about:
|     'foo bar baz'.replace(('foo', 'bar'), 'foobar')
| You can't replace multiple matches “at the same time”, as you're
| implying. The order of replacements is important, since it will affect
| the outcome in many cases.

"At the same time" might imply something equivalent to the cited
"re.sub('foo|bar',...)" suggestion. And that is different to an iterated
"replace foo, then replace bar" if the possible matched overlap.

Just a thought about what semantics the OP may have envisaged.

Personally, given re.sub and the ease of running replace a few times in
a loop, I'm -0.3 on the suggestion itself.

Cameron Simpson <cs at zip.com.au> DoD#743

A software engineering discussion from Perl-Porters:
Chip Salzenberg:        The wise one has seen the calamity,
                        and has proceeded to hide himself.
                        - Ecclesiastes
Gurusamy Sarathy:       He that observeth the wind shall not sow;
                        and he that regardeth the clouds shall not reap.

More information about the Python-ideas mailing list