1 > 0 == True -> False

Peter Otten __peter__ at web.de
Thu Jan 30 07:04:52 EST 2014


Jussi Piitulainen wrote:

> Thibault Langlois writes:
> 
>> Hello,
>> 
>> $ python
>> Python 2.7.4 (default, Sep 26 2013, 03:20:26)
>> [GCC 4.7.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> 1 > 0 == True
>> False
>> >>> (1 > 0) == True
>> True
>> >>> 1 > (0 == True)
>> True
>> >>>
>> 
>> What am I missing here ?
> 
> One or both of the following:
> 
>    >>> 0 == True
>    False
>    >>> True and False
>    False
>    >>> 1 > 0
>    True
> 
> Or the fact that (1 > 0 == True) means ((1 > 0) and (0 == True)),
> where each expression in such a chain is evaluated once, though in
> this case it really does not matter since 0 is a literal.
> 
> Hm, I don't know if the evaluation short-circuits. I think not, but
> I've never needed to know, and I don't need to know now.

It is easy to check though:

>>> def zero():
...     print("zero")
...     return 0
... 
>>> def one():
...     print("one")
...     return 1
... 
>>> def true():
...     print("true")
...     return True
... 
>>> one() > zero() == true()
one
zero
true
False
>>> zero() > one() == true()
zero
one
False

So yes, evaluation does short-curcuit.





More information about the Python-list mailing list