Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44376

Re: Comparison Style

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 <python-python-list@m.gmane.org>
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 <wlfraed@ix.netcom.com>
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> <CAPTjJmq6afVuQnXCyXNFV1QvYi_Pt2qT9tCmjBVUSvqOLunEhA@mail.gmail.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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1075.1366943839.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, 25 Apr 2013 15:57:49 +1000, Chris Angelico <rosuav@gmail.com>
declaimed the following in gmane.comp.python.general:

> On Thu, Apr 25, 2013 at 3:49 PM, llanitedave <llanitedave@veawb.coop> 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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Comparison Style llanitedave <llanitedave@veawb.coop> - 2013-04-24 22:49 -0700
  Re: Comparison Style Chris Angelico <rosuav@gmail.com> - 2013-04-25 15:57 +1000
    Re: Comparison Style llanitedave <llanitedave@veawb.coop> - 2013-04-25 07:19 -0700
      Re: Comparison Style Chris Angelico <rosuav@gmail.com> - 2013-04-26 02:25 +1000
      Re: Comparison Style Steve Simmons <square.steve@gmail.com> - 2013-04-25 19:31 +0100
        Re: Comparison Style llanitedave <llanitedave@veawb.coop> - 2013-04-25 13:13 -0700
          Re: Comparison Style Neil Cerutti <neilc@norwich.edu> - 2013-04-25 20:15 +0000
          Re: Comparison Style Steve Simmons <square.steve@gmail.com> - 2013-04-25 21:35 +0100
          Re: Comparison Style Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-04-26 10:02 +0100
  Re: Comparison Style Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-25 22:37 -0400
  Re: Comparison Style Chris Angelico <rosuav@gmail.com> - 2013-04-26 12:48 +1000
    Re: Comparison Style Roy Smith <roy@panix.com> - 2013-04-27 17:03 -0400
      Re: Comparison Style Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-27 17:40 -0400
  Re: Comparison Style Dave Angel <davea@davea.name> - 2013-04-25 23:43 -0400

csiph-web