Message92906
| Author |
georg.brandl |
| Recipients |
aronacher, belopolsky, erickt, georg.brandl, idadesub, pitrou, rhettinger, robert.kern |
| Date |
2009年09月20日.17:35:07 |
| SpamBayes Score |
2.5030164e-07 |
| Marked as misclassified |
No |
| Message-id |
<1253468108.76.0.978596738678.issue3976@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
OK, there *is* a way. Consider this:
class safe_key(object):
__slots__ = ('obj',)
def __init__(self, obj):
self.obj = obj
def __eq__(self, other):
return self.obj.__eq__(other.obj)
def __lt__(self, other):
try:
return self.obj < other.obj
except TypeError:
return id(type(self.obj)) < id(type(other.obj))
ls = [2, 1, 1.0, 1.5, 'a', 'c', 'b']
print(sorted(ls, key=safe_key)) |
|