code review

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 30 22:06:37 EDT 2012


On Sun, 01 Jul 2012 00:05:26 +0200, Thomas Jollans wrote:

> Yes. My sole point, really, is that "normally", one would expect these
> two expressions to be equivalent:
> 
> a < b < c
> (a < b) < c

Good grief. Why would you expect that?

You can't just arbitrarily stick parentheses around parts of expressions 
and expect the result to remain unchanged. Order of evaluation matters:

2**3**4 != (2**3)**4

Comparisons are no different. You can't just toss in some arbitrary 
brackets and expect the result to be the same. In the second case, the 
parentheses explicitly turn the comparison into the equivalent of this:

temporary_flag = a < b
temporary_flag < c

which is not the same as a < b < c.

This has nothing to do with chained comparisons. You can write the same 
thing without chaining:

a < b and b < c
(a < b) < c

Is it not obvious that the second case is completely different from the 
first? Chaining is irrelevant here.


> This is clearly not true. That's the inconsistency here with the rest of
> the language.

Nonsense. Your expectation that parentheses should not affect the order 
of evaluation is at odds with the entire Python language, to say nothing 
of almost every programming language in existence.


> As soon as you read it as a ternary operator, 

Well that's your problem. Why are you reading it as a ternary operator? 
It isn't one. It is a pair of chained binary operator.

How can you tell that it is a pair of binary operators, rather than a 
single ternary operator?

1) There are TWO operators, hence a pair, not a single ternary operator;

2) each operator takes TWO arguments, not three.



Chained comparisons is a standard mathematical notation with an obvious 
meaning that is familiar to anyone with even a passing familiarity to 
mathematics. They have straight-forward and simple semantics. If other 
languages don't allow them, so much the worse for other languages.

You seem to be inventing arguments to support an irrational dislike to 
the feature, but the arguments don't stand up. By all means say that you 
don't like chained comparisons, that is your right, but your attempts to 
rationalise that dislike simply do not work.


-- 
Steven



More information about the Python-list mailing list