Misleading error message of the day

Andrea Crotti andrea.crotti.0 at gmail.com
Thu Dec 8 10:55:16 EST 2011


On 12/08/2011 03:42 PM, Roy Smith wrote:
>
> 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.

Yes but how do you know how many values you generated when it quits?
I mean I don't know how it work internally, but it should keep a temporary
list of the yielded values to be able to find out how many values are 
there..



More information about the Python-list mailing list