Skip to content

Instantly share code, notes, and snippets.

@mumrah
Created July 27, 2010 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mumrah/492932 to your computer and use it in GitHub Desktop.
Save mumrah/492932 to your computer and use it in GitHub Desktop.
import cPickle as pickle
from UserDict import UserDict
class HashableDict(UserDict):
def __hash__(self):
return hash(pickle.dumps(dict([(k,self.data[k]) for k in sorted(self.data.keys())])))
>>> d1 = HashableDict({'a':1, 'b':2, 'c':3})
>>> d2 = HashableDict({'a':1, 'b':2, 'c':3})
>>> d3 = HashableDict({'a':1, 'b':2, 'c':4})
>>> set([d1,d2,d3])
set([{'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 4, 'b': 2}])
@mumrah
Copy link
Author

mumrah commented Jul 27, 2010

Set friendly dictionaries in Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment