[issue3976] pprint._safe_repr is not general enough in one instance

Georg Brandl report at bugs.python.org
Sun Sep 20 19:35:08 CEST 2009


Georg Brandl <georg at python.org> added the comment:

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))

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3976>
_______________________________________


More information about the Python-bugs-list mailing list