Endless loop

Discussion in 'Python' started by vsoler, Jan 2, 2010.

  1. vsoler

    vsoler Guest

    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
    vsoler, Jan 2, 2010
    #1
    1. Advertising

  2. vsoler

    alexru Guest

    On Jan 2, 3:50 pm, vsoler <> wrote:

    > 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.
    alexru, Jan 2, 2010
    #2
    1. Advertising

  3. vsoler wrote:
    > 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
    Ulrich Eckhardt, Jan 2, 2010
    #3
  4. vsoler

    vsoler Guest

    On 2 ene, 14:21, Ulrich Eckhardt <> wrote:
    > vsoler wrote:
    > > 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
    vsoler, Jan 2, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCb3Jn?=

    Endless loop on delete auth cookie/abandon session

    =?Utf-8?B?QmlsbCBCb3Jn?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    666
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 2, 2005
  2. Alexandre
    Replies:
    4
    Views:
    470
    Lasse Reichstein Nielsen
    Apr 12, 2005
  3. Leo
    Replies:
    1
    Views:
    406
    Thomas Heller
    Jul 4, 2003
  4. Claudio Grondi
    Replies:
    75
    Views:
    1,116
    Tim Peters
    Jan 21, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page