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


Groups > comp.lang.python > #85419

Re: __next__ and StopIteration

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'explicitly': 0.05; 'correct.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '(self):': 0.16; 'charles': 0.16; 'col': 0.16; 'container,': 0.16; 'doubly': 0.16; 'fine.': 0.16; 'generator.': 0.16; 'iteration': 0.16; 'posting,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'such,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; "i've": 0.25; 'nearly': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'raise': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'says': 0.33; 'url:python': 0.33; 'skip:_ 10': 0.34; 'indexed': 0.36; 'yield': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'remove': 0.60; 'url:3': 0.61; 'charset:windows-1252': 0.65; 'statement,': 0.68; 'batchelder': 0.84; 'subject:skip:S 10': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: __next__ and StopIteration
Date Mon, 09 Feb 2015 20:30:21 -0500
References <54D9072F.3010904@earthlink.net> <mbb1gk$i4b$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
In-Reply-To <mbb1gk$i4b$1@ger.gmane.org>
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.18587.1423531834.18130.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423531834 news.xs4all.nl 2873 [2001:888:2000:d::a6]:35926
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85419

Show key headers only | View raw


On 2/9/15 2:24 PM, Ned Batchelder wrote:
> On 2/9/15 2:14 PM, Charles Hixson wrote:
>> I'm trying to write a correct iteration over a doubly indexed container,
>> and what I've got so far is:    def __next__ (self):
>>          for row    in    range(self._rows):
>>              for col in range(self._cols):
>>                  if self._grid[row][col]:
>>                      yield    self._grid[row][col]
>>                  #end    if
>>              #end    for col
>>          #end    for row
>>          raise    StopIteration
>>
>> What bothers me is that it doesn't look like it would continue to raise
>> StopIteration if it were called again, which is what
>> https://docs.python.org/3/library/stdtypes.html#iterator.__next__ says
>> is correct.  How should this be fixed?
>
> You are using yield, which means __next__ is a generator.  As such, you
> don't have to explicitly raise StopIteration at all.  Just remove that
> statement, and you should be fine.
>
> Also, look into how you are posting, the code is nearly mangled. :(
>

Oops, __next__ isn't right here, it should be __iter__:

     def __iter__(self):
         for row in range(self._rows):
             for col in range(self._cols):
                 if self._grid[row][col]:
                     yield self._grid[row][col]

-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: __next__ and StopIteration Ned Batchelder <ned@nedbatchelder.com> - 2015-02-09 20:30 -0500

csiph-web