code review

Thomas Jollans t at jollybox.de
Sun Jul 1 03:35:40 EDT 2012


On 07/01/2012 04:06 AM, Steven D'Aprano wrote:
> On Sun, 01 Jul 2012 00:05:26 +0200, Thomas Jollans wrote:
> 
>> 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.

This is simply wrong. The comparisons are not acting as binary operators.

> 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:

As Chris points out, 2**3**4 == 2**(3**4)

For any chain of actual binary operators, it is possible to place
brackets around binary sub-expressions such that the meaning of the
total expression stays the same. This is true of ALL infix-notated
strictly binary operators. This is not true of Python comparison operators.

If you're inclined to look at operators in terms of how many arguments
they take, then the inescapable conclusion is that a chain of N
comparison operators forms one single (N+1)-term operator because it
cannot be grouped into binary expressions without rephrasing the
expression using "and".




More information about the Python-list mailing list