> 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>
Is not very helpful.
Python-ideas mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com
Message archived at https://firstname.lastname@example.org/message/2AL5FE3KZI4EBTRMJ7O5EL6MBVN7RUYF/
Code of Conduct: http://python.org/psf/codeofconduct/
Christopher Barker, PhD (Chris)
Python Language Consulting
- Scientific Software Development
- Desktop GUI and Web Development
- wxPython, numpy, scipy, Cython