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


Groups > comp.lang.python > #44323

Re: Comparison Style

References <125c8f33-1a62-4dc0-9341-a2d8f7b58058@googlegroups.com>
Date 2013-04-25 15:57 +1000
Subject Re: Comparison Style
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1052.1366869477.3114.python-list@python.org> (permalink)

Show all headers | 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