esoteric question about dict keys

Mel Wilson mwilson at the-wire.com
Fri Aug 20 07:32:18 EDT 2004


In article <mailman.2023.1092997148.5135.python-list at python.org>,
Alexis Roda <alexis.roda at urv.es> 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.

   The problem with a mutable object as a dictionary key is
that it could change in value after it had been registered
as a key, and then you would no longer find the associated
data "where you left it".  You'd have to somehow present
another key with a hash the same as the original hash, and
which compared equal to the key value after mutation.

        Regards.        Mel.



More information about the Python-list mailing list