Operator precedences

Jp Calderone exarkun at intarweb.us
Tue Feb 25 19:49:36 EST 2003


On Wed, Feb 26, 2003 at 01:04:48AM +0100, Jesper Hansen wrote:
> In what order are compare operators evaluated?
> 
> According to http://www.python.org/doc/current/ref/summary.html they all
> have the same precedence and are evaluated from right to left, but this
> does not seem to true in python 2.2.2.
> 
> These both evaluate to true, so the order does not seem to be fixed;
> (3 < 2 < 1)  ==  (3 < (2 < 1))
> (1 < 2 < 3)  ==  ((1 < 2 ) < 3)
> 
> What am I missing?

  (3 < 2) is False
  (2 < 1) is False
  (3 < (2 < 1)) is (3 < False) is (3 < 0) is False

  (1 < 2) is True
  (2 < 3) is True
  ((1 < 2) < 3) is (True < 3) is (1 < 3) is True

  Put another way,

  (x < y < z) is the same as ((x < y) and (y < z)), not either of 
((x < y) < z) or (x < (y < z)).

  It's a "weird" (maybe "uncommon" is a better word) syntax rule.

  Jp

-- 
"There is no reason for any individual to have a computer in their
home."
                -- Ken Olson, President of DEC, World Future Society
                   Convention, 1977
-- 
 up 17 days, 4:29, 6 users, load average: 0.20, 0.19, 0.22
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030225/7296c36d/attachment.sig>


More information about the Python-list mailing list