On Tue, 3 Dec 2019 at 12:48, Paul Moore <p.f.mo...@gmail.com> wrote:
>
> My impression is that he was asking for a re.findfirst(...) function
> to give a more discoverable name to the next(re.finditer((...)) idiom.
>
> As a single example of defining a dedicated function to replace a
> one-liner, I think it's marginal at best (although discoverability
> *is* important here). But IMO it is true that using
> next(some_iterator) to mean "get the first value returned" is
> something that's needed relatively frequently, but often overlooked by
> people. I'm not sure there's a good solution, though - adding an alias
> first() for "next() when used to get the first element" is probably
> overkill, and apart from dedicated syntax, it would be hard to find
> something much shorter than next().
>
> Maybe it's just an education issue, people aren't sufficiently
> familiar with the idiom?

What exactly is the idiom here?

Using bare next is not a good idea because it leaks StopIteration
which can have awkward side effects. So are you suggesting something
like

result = next(re.finditer(...), None)
if result is None:
    # raise or something
else:
    # use result

I would be in favour of adding an alternative to next that raises a
different exception when the result isn't found.

--
Oscar
_______________________________________________
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/3TH3SXC7PKM5ZFIAZTOJZW4JODVOBM6T/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to