1 > 0 == True -> False

Thibault Langlois thibault.langlois at gmail.com
Thu Jan 30 08:40:48 EST 2014


On Thursday, January 30, 2014 12:49:19 PM UTC, Dave Angel wrote:
> Thibault Langlois <thibault.langlois at gmail.com> Wrote in message:
> 
> > 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 ?
> 
> > 
> 
> > T.
> 
> > 
> 
> 
> 
> You tell us. You supply only half the question,  what it does,
> 
>  without saying what you expected or needed. 
> 
> 
> 
> I expect you're either confused about comparison chaining or about
> 
>  what happens when you compare objects of different types.
> 
>  
> 
> 
> 
> Doing an ordered comparison between two types is undefined by
> 
>  default, and not guaranteed to even give the same result between
> 
>  builds.  So the following may give different results on your
> 
>  2.7.4 than on mine.
> 
>         5 < "abc"
> 
> 
> 
> Python 3 fixes that by throwing an exception.  TypeError:
> 
>  unorderable types
> 
> 
> 
> This should solve it, since the first and third expression would
> 
>  seem to be undefined. Unfortunately there's yet another wrinkle.
> 
>  
> 
> 
> 
> For hysterical reasons,  True and False are instances of class
> 
>  bool, which is derived from int. So for comparison purposes
> 
>  False==0 and True==1. But in my opinion,  you should never take
> 
>  advantage of this, except when entering obfuscation
> 
>  contests.
> 
> 
> 
> 
> 
> -- 
> 
> DaveA

You are right. I should have given some context.
I am looking at this from the perspective of the teacher that has to explain idiosyncrasies of the language to inexperienced students.
There are two aspects in this example. 
1. the equivalence of True/False with integers 1/0 which have pro and cons.
2. the chaining rules of operators. I agree that it may make sense in some cases like x > y > z but when operators are mixed it leads to counter intuitive cases as the one I pointed out.

The recommendations to student are 1) do not assume True == 1 and do not use operator chaining.





More information about the Python-list mailing list