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

Emile van Sebille emile at fenx.com
Mon Oct 12 12:25:00 EDT 2015


On 10/12/2015 9:12 AM, Albert-Jan Roskam wrote:
> Hi,
>
>
> 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?
>
>
>
> *)http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int
>

See if implementing __cmp__ gets you what you're looking for.

See http://www.rafekettler.com/magicmethods.html#comparisons for more info.

Emile




More information about the Tutor mailing list