compare classes and not class instances

jantod at gmail.com jantod at gmail.com
Thu Mar 2 04:43:45 EST 2006


I not only want to compare class *instances* but also the classes
themselves. Something like:

class T(object):
    @classmethod
    def __cmp__(cls, other):
        return cmp(cls.__name__, other.__name__)
    @instancemethod
    def __cmp__(self, other):
        return cmp(self.x, other.x)

class B(T): pass
class A(T): pass

sorted([B, A]) => [A, B]

My motivation for doing so is simply to sort classes based on their
names and not (as it seems is the default behaviour) on the order in
which they were created.

I guess I could always just do something like sorted(classes,
key=lambda cls: cls.__name__)...but where's the fun in that? :-)




More information about the Python-list mailing list