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


Groups > comp.lang.python > #85456

Re: __next__ and StopIteration

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.032
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'expected.': 0.09; 'false,': 0.09; 'grid': 0.09; 'def': 0.12; 'suggest': 0.14; 'blank,': 0.16; 'col': 0.16; 'dict': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'iteration,': 0.16; 'iterator': 0.16; 'worst': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'example.': 0.24; 'looks': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'locations': 0.30; 'strongly': 0.30; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; 'code': 0.31; 'cells': 0.31; 'container': 0.31; 'yields': 0.31; 'class': 0.32; 'probably': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'received:209.85': 0.35; 'board': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'i.e.': 0.36; 'yield': 0.36; 'possible': 0.36; 'similar': 0.36; 'should': 0.36; 'received:209': 0.37; 'expected': 0.38; 'depends': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'remove': 0.60; 'blank': 0.60; 'full': 0.61; 'entire': 0.61; 'evaluate': 0.72; 'behavior': 0.77; '2015': 0.84; 'ethan': 0.84; 'furman': 0.84; 'subject:skip:S 10': 0.84
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=FGOqeKgTcBNRHMWXC7lX2UwvAcaMg0q1LDNiKxLVh50=; b=PV2hoGAlsmCXKw7u1ucmHBOWj2D5haESqmXTfKF2Dti+I2twia0IIqXI9/u9Z7K3jK umQyerG0yIBhWCUnwixhnuOYstgi3aUDsiXyrxc3bR2PhjVoMwzUPtE/XRObdwMDhqhB 4TYtqOFJl12WDe5R6MZHWEDeUvWD4jjN1KCOuu8lnXaWu0P4BV5awMnALwshuKm5Hc8U 4LXjyhAPuH0k5a/YPG472dmedXF8mqtl/3Q1UtQMJh3i7VLTEJMKB0w3Z1Gn4iq0NKG2 4AxPR6FqpxND2lGW2LkdvtRYZVstXkAi+sQl1KRrFFYbZBiemGIlXF26iMRoRt45T8O1 /AaQ==
X-Received by 10.68.68.139 with SMTP id w11mr39611133pbt.82.1423587278336; Tue, 10 Feb 2015 08:54:38 -0800 (PST)
MIME-Version 1.0
In-Reply-To <54DA3577.6080201@stoneleaf.us>
References <mailman.18573.1423509383.18130.python-list@python.org> <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <CALwzidnufuFPX1K0QrAJZbEtS79xvOmhM7ZZ8t9dJSiKVvhUqQ@mail.gmail.com> <54D98A35.9060203@earthlink.net> <CAPTjJmrcbOYCuZCgiq9gStEG34nAvQ2gS-vZC-WUuAOPUV3hPw@mail.gmail.com> <54DA3577.6080201@stoneleaf.us>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 10 Feb 2015 09:53:57 -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.18616.1423587280.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423587280 news.xs4all.nl 2848 [2001:888:2000:d::a6]:50065
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85456

Show key headers only | View raw


On Tue, Feb 10, 2015 at 9:44 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
> On 02/09/2015 08:46 PM, Chris Angelico wrote:
>>
>> class Grid:
>>     blah blah
>>
>>     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]
>
> I strongly suggest you remove the
>
>   if self._grid[row][col]:
>
> line.
>
> Best case scenario: the entire grid is blank, and iterating through it does nothing.
>
> Worst case scenario:  only some elements evaluate as False, so your loop doesn't execute the full number of times; i.e.
> with a grid of 4x5 with 7 blank cells you get 13 iterations -- probably not what was expected.

Depends on what the expected behavior is -- is every grid position
something that should be included in the iteration, or are we looking
at elements of a container where some possible locations may be empty?
You don't expect a dict iteration to yield empty buckets, for example.

I have some code that looks similar to this, which is an iterator for
a chess board that yields the contained pieces. It doesn't really make
sense in that case to yield empty squares.

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