On Tue, 10 Dec 2019 at 00:26, Steven D'Aprano <st...@pearwood.info> wrote: > > On Tue, Dec 10, 2019 at 10:54:10AM +1300, Greg Ewing wrote: > > > 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? > > Speaking for myself, not Guido, functions which raise are often > difficult to use, especially if you can't "Look Before You Leap", since > you have to wrap them in a try...except block to use them.
If the function allows a default to be supplied instead of raising then you don't need try/except: val = first(obj) # raises on empty val = first(obj, default) # gives default on empty That's how next works and also how first from more-itertools works. -- 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/3CS5XEJUWRON2VP32B5RODQNYJBI6N2E/ Code of Conduct: http://python.org/psf/codeofconduct/