Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3a.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'expected.': 0.09; 'false,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'grid': 0.09; 'message- id:@stoneleaf.us': 0.09; '~ethan~': 0.09; 'def': 0.12; 'suggest': 0.14; 'blank,': 0.16; 'col': 0.16; 'dict': 0.16; 'filename:fname piece:signature': 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; '>>>': 0.22; 'header:User- Agent:1': 0.23; '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; 'included': 0.31; 'code': 0.31; 'cells': 0.31; 'container': 0.31; 'cool,': 0.31; 'yields': 0.31; 'class': 0.32; 'probably': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'board': 0.35; 'something': 0.35; 'really': 0.36; 'i.e.': 0.36; 'yield': 0.36; 'thanks': 0.36; 'possible': 0.36; 'similar': 0.36; 'should': 0.36; '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; 'ian': 0.60; 'full': 0.61; 'entire': 0.61; 'received:173': 0.61; 'evaluate': 0.72; 'behavior': 0.77; '2015': 0.84; 'ethan': 0.84; 'furman': 0.84; 'subject:skip:S 10': 0.84 Date: Tue, 10 Feb 2015 09:33:23 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: __next__ and StopIteration References: <54d94307$0$12998$c3e8da3$5496439d@news.astraweb.com> <54D98A35.9060203@earthlink.net> <54DA3577.6080201@stoneleaf.us> In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="pALF5OolHwjqxmnutTCSnsSK36Jup7IHL" 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: 1423589643 news.xs4all.nl 2959 [2001:888:2000:d::a6]:34703 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85459 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --pALF5OolHwjqxmnutTCSnsSK36Jup7IHL Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 02/10/2015 08:53 AM, Ian Kelly wrote: > On Tue, Feb 10, 2015 at 9:44 AM, Ethan Furman wrot= e: >> 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 lo= op doesn't execute the full number of times; i.e. >> with a grid of 4x5 with 7 blank cells you get 13 iterations -- probabl= y not what was expected. >=20 > 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. >=20 > 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. Cool, thanks for the correction. -- ~Ethan~ --pALF5OolHwjqxmnutTCSnsSK36Jup7IHL Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBAgAGBQJU2kDpAAoJENZ7D1rrH75NFzMP/3Rik3v0xGkg8fTDOLUK1sxQ 13RO5B+iMMGPhtt6AO28dwZajykz5F5FW09nCVtdwZWiWKAyBAr92dwJ2Y9084+p TtQEhq+hfoC9yahL4dMxy3TJ/QAofsn0o/XfUtfSvEJkSukMmiZmcTAJkKfdxaKg 5RSXLbi2km6BDyEl+cWl+1nHBkTfE0Y3J8LlKMyg8FsntCT4DKZbJnP/7iti/MEr JOltuoq6QUNuz3I/J3Oz75BzTXYQLONmuFeYlR631TujyJ+jLKxhhton9kQ9VbQh EH5wQAhH66Dlqozuyc/2bff7pjne14uSxzhWtvOvxx/BUaXsWut/x6PxnafEGQbX xoqpqD7gRZMuPJEtFfvxBPPKXPOAlJwUCL7Rs8OyZoNEPvy6kVlGUna+mtmkA4Vc EtvDyPdwG1uWR83ZhTuFz1KhvJGYSxveEevpwNHQ9DzbqVceToxqp0DW6I6BpEcP sgBsPgyFJUDivYh8urbhMnWIj0YbNIzU6/mUJL8NCedqanc4iGD683dNVihTkDwu ekm+d3myrRpcaSggHkNDH2nHVQOKMqhryf+K1U2JxV+i78wDdTq2Jhfu51PrUDqh okXpT9Ks+HVELHKUTiYCh6xCq2pqhxqrpSco6FKMLfwpCQ912PhxE0Bjp3FfLeNH zZCxjMzrGyG993vZuf1P =Uv4C -----END PGP SIGNATURE----- --pALF5OolHwjqxmnutTCSnsSK36Jup7IHL--