On Tue, Oct 12, 2021 at 4:51 AM Chris Angelico firstname.lastname@example.org wrote:
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)).
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:
Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
Is not very helpful.
ChrisA _______________________________________________ Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/2AL5FE... Code of Conduct: http://python.org/psf/codeofconduct/