Endless loop

vsoler vicente.soler at gmail.com
Sat Jan 2 07:50:32 EST 2010


hello,

I'm learning Python and OOP, and I am confronted with a rather
theoretical problem.

If I run the following script:

class stepper:
    def __getitem__(self, i):
        return self.data[i]

X=stepper()
X.data="Spam"
for item in X:
    print item,

... what I get is     S p a m     which seems logical to me since the
loop stops after the 4th character.


However if I run

class stepper:
    def __getitem__(self, i):
        return i

X=stepper()
X.data="Spam"
for item in X:
    print item,

... what I get is an endless loop, starting at zero:    0 1 2 3 4 5 6
7 8 9 10 11  and so on.

My question is: why does this second script not stop after printing
number 3?  what made the first one stop while the second one will not?

Thanks for your advise

Vicente Soler



More information about the Python-list mailing list