Is it really good?

Tim Peters tim.one at comcast.net
Wed Jan 8 03:46:30 CET 2003


>>>     >>> 2 == 3 is good
>>>     0     # ????????????????????????

[Skip Montanaro]
>> Chained operations.  The above expression is effectively
>>
>>     (2 == 3) and (3 is good)
>>
>> The first is false, so the second is never evaluated.

[Grant Edwards]
> Why do you say that?

It's the "and" that short-circuits, not the "is".  Skip meant what he wrote.

> That logic would seem to indicate that
>
>   0 is 0
>
> should false, since the first term is false, the "is" short-circuits
> and returns false.

As above; "is" never short-circuits.

> ...
> Your statement also means that
>
>   >>> (2==3) is (2==3)
>   1
>
> should return false, but it doesn't.

That isn't a chained comparison.  This would be a chained comparison:

    2 == 3 is 2 == 3

and would return false, because it *means*

    (2 == 3) and (3 is 2) and (2 == 3)

> ...
> All this still doesn't explain why
>
>  (2==3) is good
>
> doesn't evaluate the right hand side of the "is"....

It does:

>>> (2 == 3) is good
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'good' is not defined
>>>

The expression

    2 == 3 is good

is not the same expression as

    (2 == 3) is good

Chained comparisons in Python usually look more like

    0 <= j < 10

and then nobody is confused, neither by the meaning, nor by that

    (0 <= j) < 10

and

    0 <= (j < 10)

are entirely different expressions.  Replace "<=" by "==" and/or "is",
though, and the differences still exist, but for some reason it's harder for
people to see that.






More information about the Python-list mailing list