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

Armin Ronacher report at bugs.python.org
Sun Sep 20 19:39:51 CEST 2009


Armin Ronacher <armin.ronacher at active-4.com> added the comment:

Eg, something like 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):
        rv = self.obj.__lt__(other.obj)
        if rv is NotImplemented:
            rv = id(type(self.obj)) < id(type(other.obj))
        return rv


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