Knowing if you can compare two objects

sebastien s.keim at laposte.net
Thu Oct 4 07:48:59 EDT 2001


My fundamental problem, is when I have a list A=[1j] later uses of the
bisect module will fail on A. So I wouldn't initiate A with an object
that can't be compared to others.

That's why I'd like to know how I can detect if an object can be
compared.

I've tried :
try:
   data<data
catch TypeError:
    XXX
but this doesn't work for lists:
>>> [1j]<[1j]
0

So, I have tried data<[data]

This work for listes:
>>> [1j]<[[1j]]
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=

But not for tupples:
>>> (1j,)<[(1,)]
0

Another problem I have is for user defined classes: one can define a
class that support the __cmp__ method but not for arbitrary objects
comparaison.

Do you know a general way to check if your object can be used in an
ordered list?

Thanks



More information about the Python-list mailing list