code review

Steven D'Aprano steve+comp.lang.python at
Mon Jul 2 03:28:25 CEST 2012

On Sun, 01 Jul 2012 16:33:15 +1000, Chris Angelico wrote:

> On Sun, Jul 1, 2012 at 4:27 PM, Steven D'Aprano
> <steve+usenet at> wrote:
>> Yes, you can find specially crafted examples where adding parentheses
>> in certain places, but not others, doesn't change the overall
>> evaluation of the expression.
> My point was that adding parentheses around the tightest-binding
> operator is a common, clear, and usually insignificant, way of
> demonstrating operator precedence. So FOR THAT USE they must not change
> evaluation of the expression. Obviously if you put them anywhere else,
> they change evaluation. Nice job knocking down a straw man.

We *really did have* somebody arguing that chained comparisons are Bad 
because you can't stick parentheses around bits of it without changing 
the semantics. That was an actual argument, not a straw-man.

It's a stupid argument, but don't blame me for pointing out it's 
stupidity. The author *assumed* that a chain of < must be left-
associative in the same way that a chain of + operators is left-
associative, but it isn't. That's an invalid and unsafe assumption -- 
even in C-like languages, there are operators which aren't left-
associative, e.g. exponentiation ** which is usually right-associative. 
(For the record, C itself doesn't have an exponentiation operator.)

When you make unsafe assumptions about an operator, and get bitten by it, 
the *correct* conclusion should be that the assumption was wrong, not 
that the language is wrong.

Technically, < in Python is left-associative: a < b < c first evaluates 
a, not b or c. But it is left-associative under the rules of comparison 
operator chaining, not arithmetic operator chaining.


More information about the Python-list mailing list