Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85458

Re: __next__ and StopIteration

References (1 earlier) <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <mailman.18582.1423526240.18130.python-list@python.org> <54d9540d$0$13003$c3e8da3$5496439d@news.astraweb.com> <CALvWhxt2u4OoKErDSQVoc4RQD=9P-_tGKnSpb-q65YB3+xWbZg@mail.gmail.com> <CALwzidmLtAGLXtrMY=OP+G_+Vjo8J7VD-FSh=qwRoC1BKoJSWQ@mail.gmail.com>
From Chris Kaynor <ckaynor@zindagigames.com>
Date 2015-02-10 09:27 -0800
Subject Re: __next__ and StopIteration
Newsgroups comp.lang.python
Message-ID <mailman.18617.1423589282.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Feb 10, 2015 at 12:30 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>
> On Mon, Feb 9, 2015 at 5:59 PM, Chris Kaynor <ckaynor@zindagigames.com> wrote:
> > On Mon, Feb 9, 2015 at 4:42 PM, Steven D'Aprano
> > <steve+comp.lang.python@pearwood.info> wrote:
> >> so that's an excellent sign that doing so is best practice, but it should
> >> not be seen as *required*. After all, perhaps you have good reason for
> >> wanting your iterable class to only be iterated over once.
> >
> > In fact, there is one in the stdlib, the "file" object, which has a
> > __iter__ which returns self. The code below shows this,
>
> Fair point. I suppose that's because the file paradigm itself has a
> concept of current position that can't easily be abstracted away.

That is basically my thought as well. It would be possible to seek
around the file while iterating (seek to the current iteration
position, read, then seek back to the previous location), however that
would slow down iteration for little practical gain. It would also
require locking and possibly introduce odd corner cases.

>
> > The "file" object is also an example of this. It is technically a
> > broken iterator according to the docs:
> >
> > Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600
> > 32 bit (Intel)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> f = open("d:/test.txt")
> >>>> iter(f) is f
> > True
> >>>> for l in f:
> > ...     print(l)
> > ...
> > line 1
> >
> > line 2
> >
> > line 3
> >
> >>>> for l in f:
> > ...     print(l)
> > ...
> >>>> f.seek(0)
> > 0
> >>>> for l in f:
> > ...     print(l)
> > ...
> > line 1
> >
> > line 2
> >
> > line 3
> >
> >>>> next(f)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > StopIteration
> >>>> f.seek(0)
> > 0
> >>>> next(f) # This should throw StopIteration as it has previously thrown StopIteration.
> > 'line 1\n'
>
> IIRC the docs also warn that mixing file iteration with other file
> methods results in undefined or broken behavior. Maybe that's only for
> Python 2 files, however.


>From the 2.7.9 docs (https://docs.python.org/2/library/stdtypes.html#file.next):
> As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right. However, using seek() to reposition the file to an absolute position will flush the read-ahead buffer.

So it explicitly states that seek, the method I used, should work
fine. I could not find any similar text in the 3.4 docs.

Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

__next__ and StopIteration Charles Hixson <charleshixsn@earthlink.net> - 2015-02-09 11:14 -0800
  Re: __next__ and StopIteration Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-02-09 19:27 +0000
  Re: __next__ and StopIteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-10 10:30 +1100
    Re: __next__ and StopIteration Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-09 16:56 -0700
      Re: __next__ and StopIteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-10 11:42 +1100
        Re: __next__ and StopIteration Chris Angelico <rosuav@gmail.com> - 2015-02-10 11:54 +1100
          Re: __next__ and StopIteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-10 12:11 +1100
            Re: __next__ and StopIteration Chris Angelico <rosuav@gmail.com> - 2015-02-10 12:58 +1100
        Re: __next__ and StopIteration Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-09 16:59 -0800
          Re: __next__ and StopIteration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-10 16:54 +1100
        Re: __next__ and StopIteration Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-10 01:30 -0700
        Re: __next__ and StopIteration Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-10 09:27 -0800
    Re: __next__ and StopIteration Charles Hixson <charleshixsn@earthlink.net> - 2015-02-09 20:33 -0800
    Re: __next__ and StopIteration Chris Angelico <rosuav@gmail.com> - 2015-02-10 15:46 +1100
    Re: __next__ and StopIteration Charles Hixson <charleshixsn@earthlink.net> - 2015-02-09 22:16 -0800
    Re: __next__ and StopIteration Chris Angelico <rosuav@gmail.com> - 2015-02-10 17:38 +1100
    Re: __next__ and StopIteration Ethan Furman <ethan@stoneleaf.us> - 2015-02-10 08:44 -0800
    Re: __next__ and StopIteration Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-10 09:53 -0700
    Re: __next__ and StopIteration Ethan Furman <ethan@stoneleaf.us> - 2015-02-10 09:33 -0800

csiph-web