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


Groups > comp.lang.python > #85439

Re: __next__ and StopIteration

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'win32': 0.03; 'broken': 0.04; 'practice,': 0.07; 'suppose': 0.07; 'thrown': 0.09; 'undefined': 0.09; 'warn': 0.09; 'python': 0.11; 'f.seek(0)': 0.16; 'iirc': 0.16; 'iterable': 0.16; 'iterated': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'once.': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'feb': 0.22; 'example': 0.22; 'previously': 0.22; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'away.': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; '(most': 0.33; 'maybe': 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; 'shows': 0.36; 'should': 0.36; 'easily': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'more': 0.64; 'results': 0.69; 'fact,': 0.69; '2014,': 0.84; '2015': 0.84; 'subject:skip:S 10': 0.84; 'technically': 0.84; 'wanting': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=IJds9zf9XoFDL/brJ589NzmLQEE1QrvV67PS/wQNaKY=; b=WvG87Aoh6hkHqYewjewkDQswPqpinlG/KQvCwcP5o7F3gb3b0yB30rKL64pSsnExf8 O2cGR4fWk+pN+73ql/CiCbXLzDtdtPu0wafDJVa2qxFQ0BuH9mCJ0Qtoj7L0QMA5NAXI hXFvKCf8j2RiWOG3oBFcKrOX1ok6inAk4tg2PMYFkmmGgNhyzHYJYWVnjxkUAbQVLYa/ 4JIVrkDDwJL12wwegBtQjBO6N8ggyTFwaNvwZY/SURY+x0avUoOlG0ydZBGAVwhvFlq5 LZXHh8nXLJWxzN165zMEJ0UPAqdGTxXZwbDqGeM7kdGiqmwHp49fomPW/62P0xjqyXC2 6ZaQ==
X-Received by 10.70.129.168 with SMTP id nx8mr11300753pdb.124.1423557064301; Tue, 10 Feb 2015 00:31:04 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CALvWhxt2u4OoKErDSQVoc4RQD=9P-_tGKnSpb-q65YB3+xWbZg@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>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 10 Feb 2015 01:30:24 -0700
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.18604.1423557073.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423557073 news.xs4all.nl 2922 [2001:888:2000:d::a6]:46152
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85439

Show key headers only | View raw


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.

> 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.

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