[Tutor] if statement problems(noob question)

Steven D'Aprano steve at pearwood.info
Mon Apr 8 13:55:17 CEST 2013


On 08/04/13 20:45, Dave Angel wrote:
>
> will always fail, since the integer 2 is not equal to the type int.  In Python 2.x, when you compare objects of different types, you generally get unequal.  (Exception, if you define your own classes, you can define how they compare to others)  In Python 3, you'd get an exception.


Not so. Equality testing is always allowed.

In Python 3:


py> 25 == "hello world"
False


exactly the same as in Python 2. It's only ordering comparisons, < <= >= > that raise an error with incompatible types:

py> 25 <= "hello world"
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() <= str()



-- 
Steven


More information about the Tutor mailing list