Well said!

On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez <juancarlo.anez@gmail.com> wrote:


On Wed, Dec 4, 2019 at 3:02 PM Guido van Rossum <guido@python.org> wrote:
Fair enough. I’ll let the OP defend his use case. 

The OP thinks that the case for wanting just the string for a first regex match, or a verifiable default if there is no match, is way too common, that the advice on the web is not very good (it should be "write a findfirst() using next() over finditer()", and that novices default to using findall(..)[0], which is troublesome. 

The proposed implementation of a findfirst() would handle many common cases, and be friendly to newcomers (why do I need to deal with a Match object?), specially if the semantics are those of findall():

     next(iter(findall(...)), default=default)
 
BTW, a common function in extensions to itertools is first():

    def first(seq, default=None):
        return next(iter(seq), default= default)

That function, first(), would also be a nice addition in itertools, and findfirst() could be implemented using it. first() avoids most use cases needing to check if a sequence or iterator is empty before using a default value. MHO is that first() deals with so many common cases that it should be a builtin.

Note that the case for findfirst() is weaker if first() is available. Yet findfirst() solves the bigger problem.

--
Juancarlo Añez
tel:+58(414)901-2021
skype:juancarloanez
--
--Guido (mobile)