[Tutor] Unexpected iterator
Patrick Sabin
patrick.just4fun at gmail.com
Thu Nov 12 10:38:28 CET 2009
Jeff R. Allen wrote:
>>>> 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...
To upack your variables a and b you need an iterable object on the right
side, which returns you exactly 2 variables, e.g you could also write
a, b = range(2)
or create your own class
class X:
def __iter__(self):
yield 0
yield 0
a,b = X()
- Patrick
More information about the Tutor
mailing list