esoteric question about dict keys

Reinhold Birkenfeld reinhold-birkenfeld-nospam at
Mon Aug 23 20:00:11 CEST 2004

Mel Wilson wrote:
> In article <mailman.2023.1092997148.5135.python-list at>,
> Alexis Roda <alexis.roda at> wrote:
>>Now the question. In "normal" dicts its not possible to use dictionaries
>>(nor other kinds of mutable objects) as keys, if I undersand correctly
>>this is a technical requirement. If I write my own dictionary-like
>>object, on wich the mutability of the keys is not a technical issue, is
>>considered blasphemous the use of dicts as keys? For example, querying
>>an SQL table can be partially modelled as a dictionary access:
>    As I understand it, you can use any object as a key if
> you give it a __hash__ method and an __eq__ or __cmp__
> method.

You can, after all, use dicts as dict keys if you create your own
subclass of dict:

class hashdict(dict):
   def __hash__(self):
      return id(self)

x = hashdict()
testdic = {x : "test"}

But in order to retrieve the "test" value, you would need to index the
testdic with exactly the same instance of the hashdict. And that's why
dictionaries are unhashable.


Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc

More information about the Python-list mailing list