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


Groups > comp.lang.python > #85458

Re: __next__ and StopIteration

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'broken': 0.04; 'explicitly': 0.05; 'position,': 0.05; 'odd': 0.07; 'practice,': 0.07; 'suppose': 0.07; 'thrown': 0.09; 'undefined': 0.09; 'warn': 0.09; 'python': 0.11; '(seek': 0.16; 'buffer,': 0.16; 'buffer.': 0.16; 'docs.': 0.16; 'f.seek(0)': 0.16; 'fine.': 0.16; 'flush': 0.16; 'iirc': 0.16; 'iterable': 0.16; 'iterated': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'once.': 0.16; 'read-ahead': 0.16; 'reposition': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'basically': 0.19; 'feb': 0.22; 'example': 0.22; 'previously': 0.22; 'mon,': 0.24; 'possibly': 0.26; 'right.': 0.26; 'read,': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; '(like': 0.30; 'absolute': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'away.': 0.31; 'consequence': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'text': 0.33; 'url:python': 0.33; '(most': 0.33; 'used,': 0.33; 'maybe': 0.34; 'received:74.125.82': 0.34; 'could': 0.34; "can't": 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'object,': 0.36; 'doing': 0.36; 'method': 0.36; 'shows': 0.36; 'possible': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'easily': 0.37; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'previous': 0.38; 'little': 0.38; 'recent': 0.39; 'received:74.125': 0.39; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'ian': 0.60; 'back': 0.62; 'more': 0.64; 'combining': 0.68; 'results': 0.69; 'fact,': 0.69; 'introduce': 0.78; '2014,': 0.84; '2015': 0.84; '3.4': 0.84; 'subject:skip:S 10': 0.84; 'technically': 0.84; 'wanting': 0.93
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=XVpNcd+urM6POw3zn3Nnw3JosdTjp1vNkYwvPTqmV00=; b=nHcron/BDzdn072wUbYGymfOBv/GtawSHCo0W44fbKDBCutoWh/KqJBieTpV4r+Dxu TOWO0fHerIEDlNKoPainXZ0Jitr25moiotop3y4ehOUNh4x/qvO56tN/bSAcYgHltbjk sMzcmJjRlMe6IS5IhwlEts7uhfgSGG4UHRUAHNg5JlzH9F7/mTL8JQcZryvm6qDYOpn1 re/eVFWS3U5oPvWX0d2FL2pH7Q6CFgYB963YcLIyZSN9a69KKJSg06x/V7IFprpYRDII IxHoRTDHS4K9fs6m8kqIbpjR4kzornswIfvYhpZm+QybcF/kKbhot0TXZUsUlcYqFabj 1F2Q==
X-Gm-Message-State ALoCoQm4s2RYlFmMwQEBnmgEj3fFTt+t+4+4et5CW0M6VwxP3DRh7u1jtCdxjqh7I7cQa2jg2s8R
X-Received by 10.180.39.35 with SMTP id m3mr37596691wik.3.1423589272427; Tue, 10 Feb 2015 09:27:52 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CALwzidmLtAGLXtrMY=OP+G_+Vjo8J7VD-FSh=qwRoC1BKoJSWQ@mail.gmail.com>
References <mailman.18573.1423509383.18130.python-list@python.org> <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 Tue, 10 Feb 2015 09:27:32 -0800
Subject Re: __next__ and StopIteration
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18617.1423589282.18130.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423589282 news.xs4all.nl 2850 [2001:888:2000:d::a6]:56162
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85458

Show key headers only | 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