Path: csiph.com!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'test,': 0.04; 'terry': 0.07; 'loop.': 0.09; 'to:addr:comp.lang.python': 0.09; 'read.': 0.10; 'wednesday,': 0.15; "'not": 0.16; 'agree.': 0.16; 'indexerror:': 0.16; 'reedy': 0.16; 'subject:Poll': 0.16; 'try/except': 0.16; 'utc,': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'say,': 0.19; 'jan': 0.19; 'cc:no real name:2**0': 0.21; 'header:In-Reply- To:1': 0.22; 'index': 0.24; 'suspect': 0.24; 'cc:2**0': 0.26; 'function': 0.27; 'not.': 0.28; "i'm": 0.28; 'pass': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'from:addr:googlemail.com': 0.31; 'does': 0.32; 'break': 0.32; 'list': 0.32; 'header:User-Agent:1': 0.33; 'loop': 0.34; 'try:': 0.34; '(not': 0.35; '...': 0.35; 'list:': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'except': 0.39; 'received:209': 0.39; 'subject: (': 0.40; 'march': 0.61; 'efficient': 0.62; 'matter': 0.62; 'making': 0.64; "'if": 0.84 Newsgroups: comp.lang.python Date: Thu, 15 Mar 2012 10:13:41 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=86.156.91.130; posting-account=HLD_OAoAAAD-0RilNRZUjdKEwXt97Q9q References: User-Agent: G2/1.0 X-Google-Web-Client: true MIME-Version: 1.0 Subject: Re: Style question (Poll) From: Jon Clements To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331831625 news.xs4all.nl 6986 [2001:888:2000:d::a6]:53738 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21695 On Wednesday, 14 March 2012 21:16:05 UTC, Terry Reedy wrote: > On 3/14/2012 4:49 PM, Arnaud Delobelle wrote: > > On 14 March 2012 20:37, Croepha wrote: > >> Which is preferred: > >> > >> for value in list: > >> if not value is another_value: > >> value.do_something() > >> break > > Do you really mean 'is' or '=='? > > If you mean x is not y, write it that way. > 'not x is y' can be misread and misunderstood, depending on whether > the 'is' is true or not. > > >>> not 1 is 1 > False > >>> not (1 is 1) > False > >>> (not 1) is 1 > False > > Does not matter how read. > > >>> not (1 is 0) > True > >>> (not 1) is 0 > False > >>> not 1 is 0 > True > > Does matter how read. > > >> if list and not list[0] is another_value: > >> list[0].do_something() > > Or > try: > value = mylist[0] > if value is not another_value: value.dosomething > except IndexError: > pass > > I would not do this in this case of index 0, but if the index were a > complicated expression or expensive function call, making 'if list' an > inadequate test, I might. > > > Hard to say, since they don't do the same thing :) > > > > I suspect you meant: > > > > for value in list: > > if not value is another_value: > > value.do_something() > > break > > > > I always feel uncomfortable with this because it's misleading: a loop > > that never loops. > > I agree. Please do not do this in public ;-). > > -- > Terry Jan Reedy I'm not sure it's efficient or even if I like it, but it avoids try/except and the use of a for loop. if next( iter(mylist), object() ) is not another_value: # ... Just my 2p, Jon.