Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'win32': 0.03; 'broken': 0.04; 'method.': 0.07; 'practice,': 0.07; 'thrown': 0.09; 'python': 0.11; 'def': 0.12; 'f.seek(0)': 0.16; 'forbid': 0.16; 'iterable': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'once.': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; 'previously': 0.22; 'reset': 0.22; 'to:name:python- list@python.org': 0.22; 'lets': 0.24; 'mon,': 0.24; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; '"",': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'received:74.125.82': 0.34; 'one,': 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; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'received:74.125': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'even': 0.60; 'skip:* 10': 0.61; 'such': 0.63; 'more': 0.64; 'fact,': 0.69; '2014,': 0.84; '2015': 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=XvdPeFFzvsPnmQ6rkE4qd/Sy9C8SgC5SRhh+8VW4HPs=; b=Uf7VQ6coLBNnR4m/bhxz/s4ZypZXFE0QESerIx7V8bMJtBDiwgfGN1o/M3hFDwjpqA SOZYD5turaRnw5jMb60la3GTQUhVxlO9RhHGtcr98ouwydiP2PKZYbMPp05NOz8jBQa4 ne7hC6Z1oRI+D11OjiY8Oib2fBCRz/XO4Dpa9XqhUDhJHygdzoVwSioWax8UUVYjmpIr /XzaJCSi9jLm+WD8w0jb4xuyJ49/mSjhh8H/K93pDIBLruYrAa/TBdE3/O5s3sUDUH/n 8vP4M7VytqWvVg2eFeT1FWrhUfE0ARwCF9kLnH+v94K9yaoNAvv4i7dvACuQZ3XMqaGR EjdA== X-Gm-Message-State: ALoCoQn3hguDwoTARShWSAkVSQ8eYzV3t3ve+Qe3gYGj9Z368T0NWmNXUQuP0U+yyxQkEdgBlJ5s X-Received: by 10.181.12.5 with SMTP id em5mr5246617wid.84.1423529977941; Mon, 09 Feb 2015 16:59:37 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <54d9540d$0$13003$c3e8da3$5496439d@news.astraweb.com> References: <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <54d9540d$0$13003$c3e8da3$5496439d@news.astraweb.com> From: Chris Kaynor Date: Mon, 9 Feb 2015 16:59:17 -0800 Subject: Re: __next__ and StopIteration To: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423529987 news.xs4all.nl 2908 [2001:888:2000:d::a6]:54710 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85417 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, There are a number of good reasons: perhaps there is no good way to reset the iterator or there is outside state that has to be managed. I'm also sure there are other reasons I cannot think of right now. > > Also, *technically* iterators may be re-iterable. The docs say that > iterators which fail to raise StopIteration forever once they are exhausted > are "broken", but the docs do not forbid broken iterators. Consenting > adults and all that. You might want an iterator with a reset() method. Even > an outright broken iterator! > > def __next__(self): > if random.random() < 0.1: raise StopIteration > return random.random() > > Why you would want one, I don't know, but if you have a hankering for such a > beast, Python lets you do it. 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' >>> Chris