hashing mutable instances (was Re: delete duplicates in list)

Thomas Heller theller at python.net
Thu Oct 30 06:20:58 EST 2003


Michael Hudson <mwh at python.net> writes:

> Alex Martelli <aleax at aleax.it> writes:
>
>> > Actually the Set in sets module has an average lookup of O(1), worst
>> > case O(n) (not 100% sure of worst case, but 99% sure). It's been
>> 
>> Hmmm -- could you give an example of that worstcase...?  a _full_
>> hashtable would give such behavior, but Python's dicts always ensure
>> the underlying hashtables aren't too full...
>
> Try inserting a bunch of instances of 
>
> class C:
>     def __hash__(self): return 0
>
> into a dictionary.

I've though about using something like this in production code
to be able to store mutable instances in a dict.
Performance problems aside (since there are only a couple of key/value
pairs in the dict), is it such a bad idea?

Thomas




More information about the Python-list mailing list