By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Is this valid ?

P: n/a
hello,

by accident I typed a double value test,
and to my surprise it seems to work.
Is this valid ?

a = 2
b = 2

a == b == 2

thanks,
Stef Mientki

Mar 19 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mar 19, 4:18 pm, Stef Mientki <stef.mien...@gmail.comwrote:
hello,

by accident I typed a double value test,
and to my surprise it seems to work.
Is this valid ?

a = 2
b = 2

a == b == 2

thanks,
Stef Mientki
It sure looks that way...

See http://www.python.org/doc/2.3.5/ref/comparisons.html for more
info.

Mike
Mar 19 '08 #2

P: n/a
Lie
On Mar 20, 4:18*am, Stef Mientki <stef.mien...@gmail.comwrote:
hello,

by accident I typed a double value test,
and to my surprise it seems to work.
Is this valid ?

a = 2
b = 2

a == b == 2

thanks,
Stef Mientki
a == b == 2
is equivalent to
a == b and b == 2
except that b is only evaluated once for the whole comparison
Mar 20 '08 #3

P: n/a
On Thu, 20 Mar 2008 15:09:08 +0100, Rolf van de Krol wrote:
John Machin wrote:
>Of course. You can chain comparisons as much as you like and is
(semi-)sensible, e.g.
Hmm, 'of course' is not the correct word for it.

Not at all. The Original Poster tried something, and it worked. There
were two alternatives:

(1) Writing a == b == 2 is valid.

(2) In the sixteen years that Python has been publicly available, with
tens of thousands or more developers using it, nobody had noticed that
Python had a bug in the compiler which incorrectly allowed a == b == 2
until Stef Mientki came along and discovered it.

Given those two alternatives, (2) would be very surprising indeed, and so
I think "of course" is well justified.

That Python allows chaining comparisons this way isn't really surprising.
That's a very natural thing to do. What's surprising is that other
languages *don't* allow chaining comparisons, but force you to write the
inefficient and (sometimes) confusing "(a == 2) and (b == 2)" instead.
--
Steven
Mar 20 '08 #4

P: n/a
On Mar 20, 6:06*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Thu, 20 Mar 2008 15:09:08 +0100, Rolf van de Krol wrote:
John Machin wrote:
Of course. You can chain comparisons as much as you like and is
(semi-)sensible, e.g.
Hmm, 'of course' is not the correct word for it.

Not at all. The Original Poster tried something, and it worked. There
were two alternatives:

(1) Writing a == b == 2 is valid.

(2) In the sixteen years that Python has been publicly available, with
tens of thousands or more developers using it, nobody had noticed that
Python had a bug in the compiler which incorrectly allowed a == b == 2
until Stef Mientki came along and discovered it.

Given those two alternatives, (2) would be very surprising indeed, and so
I think "of course" is well justified.

That Python allows chaining comparisons this way isn't really surprising.
That's a very natural thing to do. What's surprising is that other
languages *don't* allow chaining comparisons, but force you to write the
inefficient and (sometimes) confusing "(a == 2) and (b == 2)" instead.
You see a couple of occurrences in natural language-- I'm wondering
where the majority of NL settles.
>>a is a is a
True

Do we do math on booleans on some level or in some contexts? Maybe a
"with syntaxchangeA:" is in the future.

Mar 21 '08 #5

P: n/a
Lie wrote:
On Mar 20, 4:18 am, Stef Mientki <stef.mien...@gmail.comwrote:
>hello,

by accident I typed a double value test,
and to my surprise it seems to work.
Is this valid ?

a = 2
b = 2

a == b == 2

thanks,
Stef Mientki

a == b == 2
is equivalent to
a == b and b == 2
except that b is only evaluated once for the whole comparison
Yes - there seems to be unanimity.

Comparisons can be chained arbitrarily,
e.g., x < y <= z is equivalent to x < y
and y <= z, except that y is evaluated
only once (but in both cases z is not
evaluated at all when x < y is found to
be false).

Formally, if a, b, c, ..., y, z are
expressions and opa, opb, ..., opy are
comparison operators, then a opa b opb c
....y opy z is equivalent to a opa b and
b opb c and ... y opy z, except that
each expression is evaluated at most once.

Colin W.
Mar 21 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.