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: 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: <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <54D98A35.9060203@earthlink.net> <54DA3577.6080201@stoneleaf.us> From: Ian Kelly Date: Tue, 10 Feb 2015 09:53:57 -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: 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 On Tue, Feb 10, 2015 at 9:44 AM, Ethan Furman 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.