Comparisons of incompatible types

Mark Wooding mdw at distorted.org.uk
Tue Dec 7 19:09:17 EST 2010


Carl Banks <pavlovevidence at gmail.com> writes:

> I think that feeling the need to sort non-homogenous lists is
> indictative of bad design.

Here's a reason you might want to.

You're given an object, and you want to compute a hash of it.  (Maybe
you want to see whether someone else's object is the same as yours, but
don't want to disclose the actual object, say.)  To hash it, you'll need
to serialize it somehow.  But here's a problem: objects like
dictionaries and sets don't impose an ordering on their elements.  For
example, the set { 1, 'two' } is the same as the set { 'two', 1 } -- but
iterating the two might well yield the elements in a different order.
(The internal details of a hash table tend to reflect the history of
operations on the hash table as well as its current contents.)

The obvious answer is to apply a canonical ordering to unordered objects
like sets and dictionaries.  A set can be serialized with its elements
in ascending order; a dictionary can be serialized as key/value pairs
with the keys in ascending order.  But to do this, you need an
(arbitrary, total) order on all objects which might be set elements or
dictionary keys.  The order also needs to be dependent only on the
objects' serializable values, and not on any incidental facts such as
memory addresses or whatever.

-- [mdw]



More information about the Python-list mailing list