1

Comparing types that don't make sense provides a boolean result and not an Exception.

When comparing the tuple (0,) and integer 100, no Exception is raised.

I've tried comparing explicit type conversion, str casting, or repr output to produce the same comparison result, but nothing matches what is evaluated from the raw expression.

>>> (0,) > 100
True
>>> str((0,)) > str(100)
False
>>> repr((0,)) > repr(100)
False
>>> int((0,)) > int(100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'tuple'

Python should be throwing an error when trying to compare these two different objects, or there should be some implicit conversion before the compare that produces the given output.

Alec
  • 8,529
  • 8
  • 37
  • 63
  • Yes, this was an ugly feature of Python 2. Move on to Python 3 if you can, when comparisons between types that are different throws an exception. – juanpa.arrivillaga Apr 23 '19 at 23:34
  • But, in CPython 2: "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." The Python 2 *language* only states that items of different types (that don't directly support comparison) are ordered "consistently" but "arbitrarily" – juanpa.arrivillaga Apr 23 '19 at 23:35
  • An interesting, related question: https://stackoverflow.com/questions/2384078/why-is-0-true-in-python-2 – juanpa.arrivillaga Apr 23 '19 at 23:38
  • Wow, that is quite an ugly feature. I unfortunately can't move to Python 3. I'm locked into a Python 2.7 installation that is beyond my control, and stuck within a code base that will not compile/execute in Python 3. – Tyson Lewis Apr 23 '19 at 23:50
  • Thank you for pointing out that question. I don't know how long I looked for an answer before giving up and asking here. It seems that it boils down to type name or memory address for uncomparable types, regardless of how little sense that makes. – Tyson Lewis Apr 23 '19 at 23:51

0 Answers0