weird dict problem, how can this even happen?

Scott David Daniels Scott.Daniels at Acm.Org
Tue Dec 16 17:19:32 EST 2008


Joel Hedlund wrote:
> Duncan Booth wrote:
>> I think you probably are correct. The only thing I can think that 
>> might help is if you can catch all the situations where changes to the 
>> dependent values might change the hash and wrap them up: before 
>> changing the hash pop the item out of the dict, then reinsert it after 
>> the change.
> 
> That would probably require a lot of uncomfortable signal handling, 
> especially for a piece of functionality I'd like to be as unobtrusive as 
> possible in the application.
> 
>> Alternatively give up on defining hash and __eq__ for FragmentInfo and 
>> rely on object identity instead.
Perhaps your hash function could be something like:

      class HashedCache(dict):
          def __init__(self, dictionary):
              self.update(dictionary)
              self._hash = hash(tuple(sorted(dictionary.keys())))

          def __hash__(self):
              return self._hash


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list