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


Groups > comp.lang.python > #44323

Re: Comparison Style

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'static': 0.04; '(even': 0.05; 'nasty': 0.07; 'variables': 0.07; '"if': 0.09; 'operator,': 0.09; 'mostly': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'incremented': 0.16; 'loop.': 0.16; 'order?': 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; 'programming': 0.22; 'putting': 0.22; 'saying': 0.22; 'comparing': 0.24; 'earlier': 0.24; 'sort': 0.25; 'compare': 0.26; 'switch': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'possibility': 0.29; 'message- id:@mail.gmail.com': 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; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; "didn't": 0.36; 'received:209': 0.37; 'performance': 0.37; 'being': 0.38; 'convention': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'even': 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
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=dBGoVy1pbbsUmL4T5DF2kW0+0xlqIDPSUeyt2TvU4JI=; b=U1DS2pQuPEVYFqU1lCkHjcpYQ8pahraSnvcrto2RXsN0dhYALC2sZPCplU9jp4DuUF 8pz6cyTYmWPZLORgVwH3eN+EoIsnCmSqKYqvcq0JT0YCqDWcJxpca7fI+FIcZqgjFPMZ Yc0mdO60Nhvf3+Jv8morJp0RJD19XZH2mCAHIV6bW2vq+75EZV1qrXN+m9YV1r0VVcyt bTctfYIKp1DFPTlxXLmI0TFYEN9dk+Y/c4HCWk9Dr/UyoamVV+AN2lsImL3ZkhDX7mNV tsg1hK2qs/ru8/ua5TO3Z6ZZ5LkJ1S1ATtpM5B1d00aBmdL+JuFH2HA7AekBSXwkd4B4 K1OQ==
MIME-Version 1.0
X-Received by 10.58.15.193 with SMTP id z1mr26612259vec.40.1366869469071; Wed, 24 Apr 2013 22:57:49 -0700 (PDT)
In-Reply-To <125c8f33-1a62-4dc0-9341-a2d8f7b58058@googlegroups.com>
References <125c8f33-1a62-4dc0-9341-a2d8f7b58058@googlegroups.com>
Date Thu, 25 Apr 2013 15:57:49 +1000
Subject Re: Comparison Style
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-7
Content-Transfer-Encoding quoted-printable
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.1052.1366869477.3114.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366869477 news.xs4all.nl 15986 [2001:888:2000:d::a6]:50534
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44323

Show key headers only | View raw


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).

> Do I need to get a hobby?

I thought programming WAS a hobby?

ChrisA

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