Misleading error message of the day

Andrea Crotti andrea.crotti.0 at gmail.com
Thu Dec 8 09:39:27 EST 2011


On 12/08/2011 02:23 PM, Roy Smith wrote:
> 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?

I had a few errors sometimes because I thought I passed in a list while
I passed only a string, which since it's still iterable would just work
but explode later.

A nicer message wouldn't have really actually helped though, not sure
it's worth to make an exception for such a thing..



More information about the Python-list mailing list