[Python-ideas] Multiple replacement in one call [was: Give regex operations more sugar]

Ken Hilton kenlhilton at gmail.com
Thu Jun 14 07:01:11 EDT 2018


Just changing the subject line here, to keep things on topic
Sincerely,
Ken;


---------- Forwarded message ---------
Date: Thu, 14 Jun 2018 17:29:03 +1000
From: Steven D'Aprano <steve at pearwood.info>
To: python-ideas at python.org
​​
Subject: Re: [Python-ideas] Give regex operations more sugar
Message-ID: <20180614072902.GH12683 at ando.pearwood.info>
Content-Type: text/plain; charset=us-ascii

On Thu, Jun 14, 2018 at 06:33:14PM +1200, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >- should targets match longest first or shortest first? or a flag
> >  to choose which you want?
> >
> >- what if you have multiple targets and you need to give some longer
> >  ones priority, and some shorter ones?
>
> I think the suggestion made earlier is reasonable: match
> them in the order they're given. Then the user gets
> complete control over the priorities.

"Explicit is better than implicit" -- the problem with having the order
be meaningful is that it opens us up to silent errors when we neglect to
consider the order.

replace((spam, eggs, cheese) ...)

*seems* like it simply means "replace any of spam, eggs or cheese" and
it is easy to forget that that the order of replacement is *sometimes*
meaningful. But not always. So this is a bug magnet in waiting.

So I'd rather have to explicitly specify the order with a parameter
rather than implicitly according to how I happen to have built the
tuple.

# remove duplicates
targets = tuple(set(targets))
newstring = mystring.replace(targets, replacement)

That's buggy, but it doesn't look buggy, and you could test it until the
cows come home and never notice the bug.



-- 
Steve


------------------------------

Subject: Digest Footer

_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
https://mail.python.org/mailman/listinfo/python-ideas


------------------------------

End of Python-ideas Digest, Vol 139, Issue 70
*********************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180614/5f96b8ef/attachment.html>


More information about the Python-ideas mailing list