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


Groups > comp.lang.python > #85424

Re: __next__ and StopIteration

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <charleshixsn@earthlink.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'elif': 0.05; '"""': 0.07; 'column': 0.07; 'none:': 0.07; 'referring': 0.07; '(self,': 0.09; '__init__': 0.09; 'grid': 0.09; 'immutable': 0.09; 'iterate': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; 'itself.': 0.14; '(self):': 0.16; 'col': 0.16; 'constructs': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'message-id:@earthlink.net': 0.16; 'object).': 0.16; 'vars:': 0.16; 'wrote:': 0.18; 'value.': 0.19; '(the': 0.22; 'feb': 0.22; 'otherwise,': 0.22; 'header:User- Agent:1': 0.23; 'mon,': 0.24; 'source': 0.25; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; 'cells': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'trivial': 0.31; 'yourself.': 0.31; 'class': 0.32; '(e.g.': 0.33; 'skip:_ 10': 0.34; 'received:66': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'there': 0.35; 'version': 0.36; 'next': 0.36; 'method': 0.36; 'should': 0.36; 'received:209': 0.37; 'being': 0.38; 'implement': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'ian': 0.60; 'new': 0.61; 'range': 0.61; 'simply': 0.61; 'more': 0.64; 'charset:windows-1252': 0.65; 'within': 0.65; 'determine': 0.67; '2015': 0.84; 'self.value': 0.84; 'subject:skip:S 10': 0.84
DomainKey-Signature a=rsa-sha1; q=dns; c=nofws; s=dk20050327; d=earthlink.net; b=IaUX69Wqr7X5SHMAM1wyPFIALgCyHKS2QgWkjGWFYSbmUlOrf4rKFXkjh8lQHxVI; h=Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding:X-ELNK-Trace:X-Originating-IP;
Date Mon, 09 Feb 2015 20:33:57 -0800
From Charles Hixson <charleshixsn@earthlink.net>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: __next__ and StopIteration
References <mailman.18573.1423509383.18130.python-list@python.org> <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <CALwzidnufuFPX1K0QrAJZbEtS79xvOmhM7ZZ8t9dJSiKVvhUqQ@mail.gmail.com>
In-Reply-To <CALwzidnufuFPX1K0QrAJZbEtS79xvOmhM7ZZ8t9dJSiKVvhUqQ@mail.gmail.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-ELNK-Trace 8d80e6a64fbe03f9b17f27b91410de884d2b10475b571120318a924da6b73ca39da64b902c6ce0e493e42e4bc96b7432350badd9bab72f9c350badd9bab72f9c
X-Originating-IP 66.245.47.138
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.18592.1423542925.18130.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423542925 news.xs4all.nl 2967 [2001:888:2000:d::a6]:36412
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85424

Show key headers only | View raw


On 02/09/2015 03:56 PM, Ian Kelly wrote:
> On Mon, Feb 9, 2015 at 4:30 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> The way you write iterators is like this:
>>
>>
>> Method 1 (the hard way):
>>
>> - Give your class an __iter__ method which simply returns self:
>>
>>      def __iter__(self):
>>          return self
>>
>> - Give your class a __next__ method (`next` in Python 2) which *returns* a
>> value. You will need to track which value to return yourself. It must raise
>> StopIteration when there are no more values to return. Don't use yield.
>>
>>      def __next__(self):
>>          value = self.value
>>          if value is None:
>>              raise StopIteration
>>          self.value = self.calculate_the_next_value()
>>          return value
>>
>> Your class is itself an iterator.
> This is an anti-pattern, so don't even suggest it. Iterables should
> never be their own iterators. Otherwise, your iterable can only be
> iterated over once!
>
> The proper version of the "hard way" is:
>
> 1) The __iter__ method of the iterable constructs a new iterator
> instance and returns it.
>
> 2) The __iter__ method of the *iterator* simply returns itself.
>
> 3) The __next__ method of the iterator tracks the current value and
> returns the next value. Note that the iterator should never store the
> iterable's data internally, unless the iterable is immutable and the
> calculation is trivial (e.g. a range object). Instead, it should
> determine the next value by referring to its source iterable.
So if I'm understanding this correctly, I should implement as an 
internal class within Grid something like:
     class GridIter(Iterator):
         """ A class to iterate over the cells of a Grid instance
         Vars:
           row  ::  The row currently being iterated over.
           col  ::  Yhe column currently being iterated over.
           grid ::  The grid instance being iterated over.
         """

         def __init__ (self, grid):
             """
             Params:
               grid  ::  The instance of the grid to iterate over.
             """
             self.row    =    -1
             self.col    =    -1
             self.grid    =    grid
         #end    __init__

         def __iter__ (self):
             return    self

         def __next__ (self):
             if self.row == -1 and self.col == -1:
                 self.row    =    0
                 self.col    =    0
             elif self.col >= grid.cols():
                 self.row    =    self.row + 1
                 self.col    =    0
             if self.row > grid.rows():
                 raise    StopIteration
             if not    [row, col] in self.grid:
                 return    self.__next__()
             return    grid(row, col)

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