On Tue, 10 Dec 2019 at 00:26, Steven D'Aprano <steve@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