Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; '(even': 0.05; 'assignment': 0.07; 'compiler': 0.07; 'nasty': 0.07; 'variables': 0.07; '"if': 0.09; 'assigning': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'mostly': 0.14; 'c/c++': 0.16; 'expression,': 0.16; 'expression.': 0.16; 'incremented': 0.16; 'loop.': 0.16; 'order?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sane': 0.16; 'shalt': 0.16; 'tradition': 0.16; 'elements': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'normally': 0.19; 'thu,': 0.19; 'putting': 0.22; 'saying': 0.22; 'comparing': 0.24; 'url:home': 0.24; 'earlier': 0.24; 'sort': 0.25; 'compare': 0.26; 'switch': 0.26; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'wondering': 0.29; 'chris': 0.29; 'possibility': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; '25,': 0.31; 'comparison': 0.31; 'constant': 0.31; 'there,': 0.34; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'executing': 0.36; "didn't": 0.36; 'error.': 0.37; 'performance': 0.37; 'being': 0.38; 'received:76': 0.38; 'convention': 0.38; 'ends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'even': 0.60; 'then,': 0.60; 'most': 0.60; "you're": 0.61; 'first': 0.61; 'term': 0.63; 'kind': 0.63; 'such': 0.63; 'situation': 0.65; 'treating': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Comparison Style Date: Thu, 25 Apr 2013 22:37:10 -0400 Organization: > Bestiaria Support Staff < References: <125c8f33-1a62-4dc0-9341-a2d8f7b58058@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-76-249-23-65.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366943839 news.xs4all.nl 15917 [2001:888:2000:d::a6]:50464 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44376 On Thu, 25 Apr 2013 15:57:49 +1000, Chris Angelico declaimed the following in gmane.comp.python.general: > On Thu, Apr 25, 2013 at 3:49 PM, llanitedave wrote: > > Given that > > > > s = some static value > > i = a value incremented during a loop > > > > I'm used to comparing them as > > > > if i == s: > > # some code > > > > But for some unknown reason I did a switch > > > > if s == i: > > # same code > > > > It didn't seem to make any difference at first glance, so I just got to wondering -- > > It won't make any difference in any sort of sane code. If there's any > situation in which == is not reflexive, something seriously nasty is > going on. > > > Is there a standard for comparison order? Is there any kind of performance difference? Is there even a tradition for one or the other? Are there any gotchas? > > It's conventional to compare variables to constants, not constants to > variables (even in C where there's the possibility of mucking up the > operator, most people still compare variables to constants). I'd > normally use "i == s" there, treating s as a constant for the purpose > of the loop. Unless you're deliberately being poetical, language such > as "Three is the number thou shalt count" is distinctly abnormal, so > saying "if (5 == i)" is equally awkward. It's nothing major; mostly > it's like the algebraic convention of putting the more-known elements > earlier in a term (eg 2šix - 2 is known, 3.14159.... is mostly known, > i is imaginary but at least it's constant, and x is unknown). > The main reason for /literal/ first is to trap C/C++ assignment as expression. if (x = 5) ends up assigning x the value 5 and THEN, since 5 is "true" executing the "then" part. if (5 = x) OTOH is a compiler error. But since Python doesn't allow assignment/binding as an expression, either would be flagged an error. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/