Endless loop

V

vsoler

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

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
 
A

alexru

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?

First one will raise IndexError when string is over, second one won't.
 
U

Ulrich Eckhardt

vsoler said:
class stepper:
def __getitem__(self, i):
return self.data

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.


I think you're mistaking the cause and effect here, see below.
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?

First thing to observe in the second case is that X's data field is not used
anywhere. The field is set and then not used any further. In particular, it
is not used to somehow get the length of the sequence to return.

Now, "spam"[4] will raise an IndexError, thus terminating the iteration. The
second implementation which only returns the index never will do that, so
the iteration never stops.

Uli
 
V

vsoler

vsoler said:
class stepper:
    def __getitem__(self, i):
        return self.data

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.

I think you're mistaking the cause and effect here, see below.
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?

First thing to observe in the second case is that X's data field is not used
anywhere. The field is set and then not used any further. In particular, it
is not used to somehow get the length of the sequence to return.

Now, "spam"[4] will raise an IndexError, thus terminating the iteration. The
second implementation which only returns the index never will do that, so
the iteration never stops.

Uli


It's clear, I now understand.

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top