Misleading error message of the day
Benjamin Kaplan
benjamin.kaplan at case.edu
Thu Dec 8 14:38:28 EST 2011
On Thu, Dec 8, 2011 at 2:09 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> Benjamin Kaplan wrote:
>>
>> On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith <roy at panix.com> wrote:
>>>
>>> (some,
>>> very,
>>> long,
>>> list,
>>> of,
>>> variable,
>>> names,
>>> to,
>>> get,
>>> the,
>>> stuff,
>>> unpacked,
>>> into) = function_that_should_return_a_14_tuple()
>>>
>>> raises
>>>
>>> ValueError: too many values to unpack
>>>
>>> Quick, what's the bug? Did I forget a variable on the LHS, or is my
>>> function returning more things than it should? I know it's supposed to be
>>> 14, but I don't know which side is wrong. Had it said "... expected 13, got
>>> 14", I would know immediately.
>>>
>>
>> If the RHS was a tuple or a list, yes you could know immediately. But
>> unpacking works with any iterable, so it probably doesn't special-case
>> lists and tuples. Iterables don't have a size- they just keep going
>> until StopIteration is raised. So in EVERY SINGLE CASE, you would get
>> "expected n args, got n+1" even if the iterable would return 24 items
>> instead of 14, or would never stop returning items.
>
>
> Not so. There could be fewer, in which you could see "expected 13 args, got
> 7."
>
You mean like this?
>>> a,b,c = ['a','b']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
More information about the Python-list
mailing list