custom sorting and __cmp__

Kristian Ovaska kristian.ovaska at
Sun Nov 30 18:04:06 CET 2003

Lee Harr <missive at>:
>Should I be catching comparisons to objects that do not have
>my 'level' attribute and falling back to id comparison?

I usually solve it like this:

class A:
    def __init__(self, level):
        self.level = level

    def __cmp__(self, other):
        if isinstance(other, A):
            return cmp(self.level, other.level)
            return -1

Kristian Ovaska -

More information about the Python-list mailing list