I am writing a loop over 4 list-iterators like this :
it0 = iter(['foo','bar'])
it1 = iter(['bar','foo'])
it2 = iter(['foo','foo'])
it3 = iter(['bar','bar'])
try:
a, b, c, d = map(next, (it0, it1, it2, it3))
while True:
#some operations that can raise ValueError
if a+b+c+d == 'foobar'*2:
a, b, c, d = map(next, (it0, it1, it2, it3))
elif a+b == 'foobar':
c, d = map(next,(it2, it3))
else:
a, b = map(next,(it0, it1))
except StopIteration:
pass
But when I run this code in Python3, I get a ValueError: not enough values to unpack (expected 2, got 0)
raised before the expected StopIteration
.
I do not want to catch ValueError
in the except
statement because some operations in the while loop could also result in a ValueError
that should stop the program.
How is it that next
doesn't raise its exception before the assignement ?
In Python2.7, StopIteration
is raised first.