On Wed, Dec 4, 2019 at 3:02 PM Guido van Rossum <gu...@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*
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/M2OZCN5C26YUJJ4EXLIIXHQBGF6IM5GW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to