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: 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: References: <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <54d9540d$0$13003$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Tue, 10 Feb 2015 01:30:24 -0700 Subject: Re: __next__ and StopIteration To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Mon, Feb 9, 2015 at 5:59 PM, Chris Kaynor wrote: > On Mon, Feb 9, 2015 at 4:42 PM, Steven D'Aprano > 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 "", line 1, in > 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.