On Tue, Oct 12, 2021 at 4:51 AM Chris Angelico
Exactly: simple usage of next is often a bug. We need to be careful about this every time someone suggests that it's straight-forward to do next(iter(obj)).
<snip>
Please give a real example of where calling first() and getting ValueError is safer than calling next(iter(x)) and getting StopIteration. So far, I am undeterred in believing that the two exceptions have equivalent effect if the caller isn't expecting them.
I don't know about safer, but it is a clear example of why using next(iter(obj)) requires a pretty complete knowledge of the iteration protocol. I can guarantee you I'd get some questions from my students when they got a StopIterationError! If one DID write a first() function, it maybe or maybe not should raise a different exception, but it should certainly provide a better error message:
next(iter([])) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
Is not very helpful. -CHB
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/2AL5FE... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython