Feed on

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.


>>> 1j*2
>>> 1j*2 < b
Traceback (most recent call last):
File "“, line 1, in
TypeError: no ordering relation is defined for complex numbers

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

One Response to “breaking pythons comparisons”

  1. on 23 Sep 2009 at 10:12 pm Charles Plager

    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…

Did I get this wrong? Let me know!

Trackback URI | Comments RSS