
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)

On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez <juancarlo.anez@gmail.com> wrote: 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.
I agree that there should be a builtin like first but to be clear it should not raise StopIteration if the iterator turns out to be empty. Probably it should raise ValueError analogous to
a, *_ = [] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: not enough values to unpack (expected at least 1, got 0)
-- Oscar

On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez <juancarlo.anez@gmail.com <mailto:juancarlo.anez@gmail.com>> wrote: 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)
Could you show us a real code that uses it?
participants (3)
-
Guido van Rossum
-
Oscar Benjamin
-
Serhiy Storchaka