Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44376
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Comparison Style |
| Date | 2013-04-25 22:37 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <125c8f33-1a62-4dc0-9341-a2d8f7b58058@googlegroups.com> <CAPTjJmq6afVuQnXCyXNFV1QvYi_Pt2qT9tCmjBVUSvqOLunEhA@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1075.1366943839.3114.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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