[Tutor] Unexpected iterator

Dave Angel davea at ieee.org
Thu Nov 12 16:33:41 CET 2009

Jeff R. Allen wrote:
> Hello,
> I am working my way through the tutorial, and I like trying
> variations, just to see what expected errors look like, and other ways
> things could be written.
> I tried a, b = 0, 0 and that worked.
> Then I tried this to (maybe) set both a and b to 0:
>>>> a, b = 0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
> I understand why it doesn't work, but I don't understand the wording
> of the exception. Could someone explain how I accidentally introduced
> iteration into the picture with my syntax? I have a feeling there's an
> interesting lesson hidden in this example...
> Thanks.
>   -jeff
What you've really got going across the = sign is a list, or something 
like it.  On the left side, you have exactly two items, so on the right 
side you need a list or tuple of length 2.  In your first example, you 
have a tuple, because of the comma.

The generalization implied above is an iterable.  Lists and tuples are 
iterables, but you could also use a generator, for example.  Anything 
that you could iterate over, such as with a for statement.

a, b, c, d = xrange(4)

Anyway, in your second example, the system is trying to interpret the 
right side as an iterable, in order to get two items from it.


More information about the Tutor mailing list