On Sun, Dec 8, 2019 at 2:23 PM Oscar Benjamin <oscar.j.benja...@gmail.com> wrote:
> On Sun, 8 Dec 2019 at 18:42, Guido van Rossum <gu...@python.org> wrote: > > > > We're not changing next(). It's too fundamental to change even subtly. > > I don't think that anyone has proposed to change the behaviour of > next. I have suggested that if there is to be a new function very > similar to next then it can also solve another problem with next which > is the case where there should be no default and an empty iterator > should raise (something other than StopIteration). > Isn't that much less common? > > We might add itertools.first(), but not builtins.first(). This kind of > functionality is not fundamental but it's easy to get slightly wrong > (witness many hasty attempts in these threads). > > > > itertools.first() should be implemented in C, but its semantics should > be given by this (well, let me see if I can get it right): > > > > def first(it, /, default=None): > > it = iter(it) > > try: > > return next(it) > > except StopIteration: > > return default > > This version assumes a default default of None so it can't be used to > raise on an empty iterable: > > >>> print(first([])) > None > The whole point of first() would be to make it *not* raise. -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ 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/JVQYI34GQKPCC7B43JQFZ2CWC6ESH3UO/ Code of Conduct: http://python.org/psf/codeofconduct/