Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'example:': 0.03; 'matches': 0.07; 'nested': 0.07; 'string': 0.09; 'logic': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; "wouldn't": 0.14; 'changes': 0.15; '"run': 0.16; 'better:': 0.16; 'expensive,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'length.': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'reedy': 0.16; 'roy': 0.16; 'sentinel': 0.16; 'simpson': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'zero,': 0.16; 'zero.': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; 'test.': 0.24; 'decide': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'order.': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; 'easier': 0.31; "d'aprano": 0.31; 'faster,': 0.31; 'steven': 0.31; '"the': 0.34; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'example,': 0.37; 'being': 0.38; 'sometimes': 0.38; 'received:211': 0.38; 'handle': 0.38; 'short': 0.38; 'structure': 0.39; 'how': 0.40; 'even': 0.60; 'easy': 0.60; 'life,': 0.60; 'worry': 0.60; 'matter': 0.61; 'course': 0.61; "you're": 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'real': 0.63; 'become': 0.64; 'more': 0.64; 'worth': 0.66; 'benefit': 0.68; 'smith': 0.68; 'article': 0.77; 'premature': 0.84; 'response,': 0.91; 'free!': 0.95; '2013': 0.98 Date: Mon, 27 May 2013 11:57:54 +1000 From: Cameron Simpson To: Steven D'Aprano Subject: Re: Short-circuit Logic MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.21 (2010-09-15) References: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.0 cv=e/de0tV/ c=1 sm=1 a=wom5GMh1gUkA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=EnxjxczguSsA:10 a=kZ7UWmmPAAAA:8 a=8AHkEIZyAAAA:8 a=XlF_uX-gaTy35Ozn7-8A:9 a=CjuIK1q_8ugA:10 a=pyH5b1fOeEsA:10 a=VhVvL8HfBcoA:10 a=ChdAjXE5lkUvdteQbhpnkQ==:117 Cc: python-list@python.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369621763 news.xs4all.nl 15887 [2001:888:2000:d::a6]:32775 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46156 On 27May2013 00:40, Steven D'Aprano wrote: | On Sun, 26 May 2013 16:22:26 -0400, Roy Smith wrote: | | > In article , | > Terry Jan Reedy wrote: | > | >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: | >> | >> > if not allow_zero and abs(x) < sys.float_info.epsilon: | >> > print("zero is not allowed") | >> | >> The reason for the order is to do the easy calculation first and the | >> harder one only if the first passes. | > | > This is a particularly egregious case of premature optimization. You're | > worried about how long it takes to execute abs(x)? That's silly. | | I don't think it's a matter of premature optimization so much as the | general principle "run code only if it needs to run". Hence, first you | check the flag to decide whether or not you care whether x is near zero, | and *only if you care* do you then check whether x is near zero. | | # This is silly: | if x is near zero: | if we care: | handle near zero condition() | | # This is better: | if we care: | if x is near zero | handle near zero condition() | | | Not only is this easier to understand because it matches how we do things | in the real life, but it has the benefit that if the "near zero" | condition ever changes to become much more expensive, you don't have to | worry about reordering the tests because they're already in the right | order. I wouldn't even go that far, though nothing you say above is wrong. Terry's assertion "The reason for the order is to do the easy calculation first and the harder one only if the first passes" is only sometimes that case, though well worth considering if the second test _is_ expensive. There are other reasons also. The first is of course your response, that if the first test fails there's no need to even bother with the second one. Faster, for free! The second is that sometimes the first test is a guard against even being able to perform the second test. Example: if s is not None and len(s) > 0: ... do something with the non-empty string `s` ... In this example, None is a sentinel value for "no valid string" and calling "len(s)" would raise an exception because None doesn't have a length. With short circuiting logic you can write this clearly and intuitively in one line without extra control structure like the nested ifs above. Cheers, -- Cameron Simpson Who are all you people and why are you in my computer? - Kibo