[Tutor] 0 > "0" --> is there a "from __future__ import to make this raise a TypeError?

Danny Yoo dyoo at hashcollision.org
Tue Oct 13 14:29:00 EDT 2015


> In Python 2 one can do silly apple-pear comparisons such as 0> "0".*) "CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.". In Python3  this has been fixed (it raises a TypeError). Is there a way to emulate this behavior in Python 2?


I don't think so, at least, not easily.

I think that you probably want to move to Python 3 if possible
specifically because it cleans up issues like this.  There are a few
niggling differences between Python 2 and 3, most of them with the
same characteristic as the one you're pointing out with loose
comparison.  (e.g. distinguishing bytes vs strings is another one of
the fixes in Python 3).   And if you're willing to change the
semantics of "<" in Python 2, you're already well on your way to
Python 3.


More information about the Tutor mailing list