code review
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Jul 1 20:43:44 EDT 2012
On Sun, 01 Jul 2012 09:35:40 +0200, Thomas Jollans wrote:
> 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.
Of course they are. Take this chained comparison:
a < b == c
There are exactly TWO operators. Each one takes TWO arguments.
The < operator takes a and b as arguments. That's TWO, not three.
The == operator takes b and c arguments. Again, that's TWO, not three.
If you want to claim that this is a ternary operator, you need to explain:
1) What is the operator in this expression? Is it < or == or something
else?
2) What double-underscore special method does it call? Where is this
mysterious, secret, undocumented method implemented?
3) Why do the Python docs lie that a < b == c is exactly equivalent to
the short-circuit expression (a < b) and (b == c) with b evaluated once?
4) And how do you explain that the compiled byte code actually calls the
regular two-argument binary operators instead of your imaginary three-
argument ternary operator?
py> from dis import dis
py> dis(compile("a < b == c", "", "single"))
1 0 LOAD_NAME 0 (a)
3 LOAD_NAME 1 (b)
6 DUP_TOP
7 ROT_THREE
8 COMPARE_OP 0 (<)
11 JUMP_IF_FALSE_OR_POP 23
14 LOAD_NAME 2 (c)
17 COMPARE_OP 2 (==)
20 JUMP_FORWARD 2 (to 25)
>> 23 ROT_TWO
24 POP_TOP
>> 25 PRINT_EXPR
26 LOAD_CONST 0 (None)
29 RETURN_VALUE
--
Steven
More information about the Python-list
mailing list