Misleading error message of the day

Roy Smith roy at panix.com
Thu Dec 8 09:23:33 EST 2011


I just spent a while beating my head against this one.

# Python 2.6
>>> a, b = 'foo'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

The real problem is that there's too *few* values to unpack!  It should 
have been

a, b = 'foo', 'bar'

I understand why it's generating the exception it does (the string is an 
iterable), but man, did that message throw off my thought processes and 
lead me down some totally bogus debugging paths.

It's an unusual case to want to unpack a string.  Maybe the message 
should changed to "too {many, few} values to unpack (are you sure you 
wanted to unpack a string?)" if the RHS is a basestring?



More information about the Python-list mailing list