ian.g.kelly at gmail.com
Mon Jul 2 21:16:26 CEST 2012
On Sat, Jun 30, 2012 at 2:50 PM, Thomas Jollans <t at jollybox.de> wrote:
> Which of the two comparisons is done first anyway?
> "In the face of ambiguity, refuse the temptation to guess."
I would consider that a pro, not a con, because the C-like way is much
worse in this regard. Using operator chaining, is "1 < 2 < 3"
1 < 2 and 2 < 3 # assuming left-to-right evaluation order for "and"
2 < 3 and 1 < 2
The former seems pretty obvious to me (since it more closely matches
the original syntax) and also turns out to be correct, but more to the
point, most of the time it really doesn't matter which is evaluated
first. It's only relevant if:
a) your comparison operator has side-effects (bad programmer! bad!); or
b) one of the comparisons is significantly faster than the other --
but since usually both comparisons will be of the same type (e.g. both
comparing two numbers), this is also a corner case.
On the other hand, using the C-like interpretation, is "1 < 2 < 3"
(1 < 2) < 3
1 < (2 < 3)
I would guess the former is more common, but I really have no basis
for that guess beyond some experience with languages that use this
syntax. I can see no particular advantages to either interpretation
and can certainly imagine that some languages might choose the latter
instead. Moreover, the distinction actually matters in this case,
because the first expression is true (at least in Python) while the
second is false.
I will take ambiguity that is mostly unimportant over ambiguity that
is critical to the meaning of the expression any day of the week.
More information about the Python-list