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

INADA Naoki songofacandy at gmail.com
Thu Apr 12 13:32:45 CEST 2012


I want multiple replace at once. For example html escape looks like:

>>> "<>&".replace('<', '<', '>', '>', '&', '&')
'<>&'

or

>>> "<>&".replace( ('<', '<'), ('>', '>'), ('&', '&') )
'<>&'


On Thu, Apr 12, 2012 at 6:39 PM, Tshepang Lekhonkhobe
<tshepang at gmail.com> wrote:
> On Thu, Apr 12, 2012 at 06:37, Ben Finney <ben+python at benfinney.id.au> wrote:
>> Cameron Simpson <cs at zip.com.au> writes:
>>
>>> "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.
>>
>> Yes, it is; but the OP presented a proposal as though it were to have
>> the same semantics as a sequence of replace operations.
>>
>> If the OP wants to specify different semantics, let's hear it.
>
> You guys are thinking more deeply about this than I was. I don't even
> see a difference between the 2:
>
>>>> 'foo bar baz'.replace('foo', 'baz').replace('bar', 'baz') == re.sub('foo|bar', 'baz', 'foo bar baz')
> True
>
> I was not even thinking about ordering, but it would help to have it
> to avoid confusion I think. The example I gave was just the closest I
> could think of.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



-- 
INADA Naoki  <songofacandy at gmail.com>



More information about the Python-ideas mailing list