[Python-Dev] Tuples and underorderable types

Scott Dial scott+python-dev at scottdial.com
Fri Apr 24 20:25:04 CEST 2009


Raymond Hettinger wrote:
> Would it make sense to provide a default ordering whenever the types are
> the same?
> 
>    def object.__lt__(self, other):
>            if type(self) == type(other):
>                 return id(self) < id(other)
>            raise TypeError

No. This only makes it more difficult for someone wanting to behave
smartly with incomparable types. I can easily imagine someone wanting
incomparable objects to be treated as equal wrt. sorting. I am thinking
especially with respect to keeping the sort stable. I think many
developers would be surprised to find,

 >>> a =
 >>> tasks = [(10, lambda: 0), (20, lambda: 1), (10, lambda: 2)]
 >>> tasks.sort()
 >>> assert tasks[0][1]() == 0

, is not guaranteed.

Moreover, I fail to see your point in general as a bug if you accept
that there is not all objects can be total ordered. We shouldn't be
patching the object base class because of legacy code that relied on
sorting tuples; this code should be updated to either use a key function.

-Scott

-- 
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu


More information about the Python-Dev mailing list