How to get Python 2 to raise an exception when comparing different types?

Comparing strings with ints in Python 2.7.2 looks very incompatible:

x = '12'; y = 3
print x > y      # True
x = 12; y = '3'
print x > y      # False

According How does Python compare string and int? in Python 3, this will throw an exception. Is there a way to make Python 2 already behave like this? Looking at __future__I could not understand if there is such an opportunity.

+4
source share
2 answers

No. Your choice:

  • Record and use wrapper classes that result from unwanted mappings.

  • Record and use custom comparison functions instead of regular operators.

  • - MacroPy, .

  • .

  • .

  • Python 3, Python 3.

+4

( , C), . TypeError .

, Python 2, Python 3, .

+2

All Articles