To put it simple, unpacking raises ValueError:
>>> x, = ()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 1, got 0)
>>> x, = (1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 1)
But if the iterator raises ValueError, there's no way to tell it apart
from the unpacking:
>>> def foo():
... yield None
... raise ValueError
...
>>> foo()
<generator object foo at 0x7fa0e70e6430>
>>> x = foo()
>>> x, = foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in foo
ValueError
And the workaround for this is a bit ugly. We already convert e.g.
StopIteration into RuntimeError in many cases, why can't we do so here too?
For backwards compatibility, this should probably be an itertools
utility tho.
_______________________________________________
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/WTINS4QYQMVUI3UF7IC5ZJSFINA3YTL7/
Code of Conduct: http://python.org/psf/codeofconduct/