breaking pythons comparisons
October 5th, 2006 by Lawrence David
i was trying to debug some code the other day by throwing an error whenever a comparison was performed. problem was, python will compare almost anything without crashing; it’ll compare lists to strings, integers to dicts, and not complain at all.
it took some googling, but i finally found something python didn’t like to compare: complex numbers.
observe:
>>> 1j*2
2j
>>> 1j*2 < b
Traceback (most recent call last):
File "“, line 1, in
TypeError: no ordering relation is defined for complex numbers
The reason that python doesn’t like to compare complex numbers is because there’s no way to do it.
Which is bigger, 5 or 6*J? 5 has a bigger real component, but 6J has a bigger magnitude. There’s no rule as to which one is right…