Misleading error message of the day

Roy Smith roy at panix.com
Thu Dec 8 10:42:46 EST 2011


On Thursday, December 8, 2011 10:16:56 AM UTC-5, Heiko Wundram wrote:
> Am 08.12.2011 15:47, schrieb Robert Kern:
> > Would including the respective numbers help your thought processes?
> > ValueError: too many values to unpack (expected 2, got 3)
> 
> Not possible in the general case (as the right-hand side might be an 
> arbitrary iterable/iterator...).

Why not?  Take this example:

def i():
    i = 0
    while True:
        print "returning:", i
        yield i
        i += 1

a, b = i()

./iter.py
returning: 0
returning: 1
returning: 2
Traceback (most recent call last):
  File "./iter.py", line 10, in <module>
    a, b = i()
ValueError: too many values to unpack

The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct.  Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown.



More information about the Python-list mailing list