discarded iterator.next() at interactive global scope doesn't bump interator??

Discussion in 'Python' started by Bengt Richter, Sep 4, 2005.

  1. Is this a well known bug that's been fixed?
    I couldn't find any discussion of it, but maybe my googling's off today ;-/

    >>> def foo():

    ... it = iter(range(10))
    ... while True:
    ... i = it.next()
    ... print i
    ... if i&3==0:
    ... print 'skipping next'
    ... it.next()
    ...
    >>>


    ------------------< This looks ok:
    >>> foo()

    0
    skipping next
    2
    3
    4
    skipping next
    6
    7
    8
    skipping next
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 4, in foo
    StopIteration
    >>>

    ------------< now at interactive global scope:
    >>> it = iter(range(10))
    >>> while True:

    ... i = it.next()
    ... print i
    ... if i&3==0:
    ... print 'skipping next'
    ... it.next()
    ...
    -----<this doesn't look ok: it.next() is apparently not doing anything:
    0
    skipping next
    1
    2
    3
    4
    skipping next
    5
    6
    7
    8
    skipping next
    9
    Traceback (most recent call last):
    File "<stdin>", line 2, in ?
    StopIteration

    ----------------------------< but add a print, and all is well??
    >>> it = iter(range(10))
    >>> while True:

    ... i = it.next()
    ... print i
    ... if i&3==0:
    ... print 'skipping next'
    ... print '-->', it.next()
    ...
    0
    skipping next
    --> 1
    2
    3
    4
    skipping next
    --> 5
    6
    7


    8
    skipping next
    --> 9
    Traceback (most recent call last):
    File "<stdin>", line 2, in ?
    StopIteration

    I guess it could be in the read-eval-print loop

    I have an old 2.4 on NT4 and am running it in a console window.
    Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
    [GCC 3.2.3 (mingw special 20030504-1)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

    Regards,
    Bengt Richter
     
    Bengt Richter, Sep 4, 2005
    #1
    1. Advertising

  2. Bengt Richter

    Peter Otten Guest

    Bengt Richter wrote:

    [it.next() appears to be a noop in the interactive interpreter]

    > I guess it could be in the read-eval-print loop


    Indeed:

    >>> for i in range(5):

    .... 42
    ....
    42
    42
    42
    42
    42

    Whereas:

    >>> for i in range(5):

    .... None
    >>>


    Every line with an expression that doesn't evaluate to None is echoed.
    Therefore

    it.next()

    and

    print it.next()

    look the same when repr(it.next()) == str(it.next()).

    No bug :)

    Peter
     
    Peter Otten, Sep 4, 2005
    #2
    1. Advertising

  3. On Sun, 04 Sep 2005 10:10:33 +0200, Peter Otten <> wrote:

    >Bengt Richter wrote:
    >
    >[it.next() appears to be a noop in the interactive interpreter]
    >
    >> I guess it could be in the read-eval-print loop

    >
    >Indeed:
    >
    >>>> for i in range(5):

    >... 42
    >...
    >42
    >42
    >42
    >42
    >42
    >
    >Whereas:
    >
    >>>> for i in range(5):

    >... None
    >>>>

    >
    >Every line with an expression that doesn't evaluate to None is echoed.
    >Therefore
    >
    >it.next()
    >
    >and
    >
    >print it.next()
    >
    >look the same when repr(it.next()) == str(it.next()).
    >
    >No bug :)
    >

    D'oh. I said it, but I didn't hear myself ;-/

    Regards,
    Bengt Richter
     
    Bengt Richter, Sep 4, 2005
    #3
    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. George Lake

    bump shell commands via ASP

    George Lake, May 11, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    409
    George Lake
    May 11, 2006
  2. Remarkable
    Replies:
    1
    Views:
    324
  3. Deniz Bahar
    Replies:
    2
    Views:
    475
    Andrey Tarasevich
    Mar 9, 2005
  4. Replies:
    3
    Views:
    444
    red floyd
    Apr 3, 2006
  5. brez!  !!
    Replies:
    8
    Views:
    98
    Ross Bamford
    Mar 29, 2006
Loading...

Share This Page