Python 3.0 - is this true?
"Martin v. Löwis"
martin at v.loewis.de
Sun Nov 9 13:42:46 EST 2008
> In any case, would it be possible to add a cmp= function which did more
> or less what the current default cmp does now?
As somebody pointed out, it's possible to create a key= function that
provides arbitrary ordering: just return an object custom type whose
__lt__ never fails:
class AlwaysOrdered:
def __init__(self, o):
self.o = o
def __lt__(self, other):
o1 = self.o
o2 = other.o
try:
return o1 < o2
except TypeError:
return cmp((type(o1).__name__, id(o1)),
(type(o2).__name__, id(o2)))<0
L.sort(key=AlwaysOrdered)
(This is not quite the current implementation, and not even
transitive, but you get the idea)
Regards,
Martni
More information about the Python-list
mailing list