On 9/12/19 7:37 am, Guido van Rossum wrote:

def first(it, /, default=None):
     it = iter(it)
     try:
         return next(it)
     except StopIteration:
         return default

Can you provide any insight into why you think it's better for
it never to raise an exception, as opposed to raising something
other than StopIteration when the iterator is empty and no
default is specified?

There seem to be two kinds of use case for this:

1. The iterator may or may not be empty, and you don't want
the hassle of having to catch an exception.

2. You expect the iterator to never be empty; if it is, then
it's a bug, and you would like to get an exception, but not
StopIteration because that can mess other things up.

Your version of the function seems to be aimed exclusively
at case 1. If it were to raise ValueError on an empty
iterable unless a default were explicitly given, it would
address both cases.

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

Reply via email to