James Stroud jstroud at
Fri Jan 13 19:26:22 EST 2006

bearophileHUGS at wrote:
> Dicts and sets require immutable keys, like tuples or frozensets

Not really...

def freeze(anobj):
   """returns a new hashable object"""
   import copy
   try: hash(anobj)
   except: pass
   else: return copy.deepcopy(anobj)
   class FrozenType(type):
     def __new__(cls, name, bases, dct):
       return type.__new__(cls, name, bases, dct)
     def __init__(cls, name, bases, dct):
       super(FrozenType, cls).__init__(name, bases, dct)
   def hashself(self): return hash(repr(self))
   name = 'Frozen_%s' % anobj.__class__.__name__
   bases = (anobj.__class__,)
   dct = dict(anobj.__class__.__dict__)
   dct['__hash__'] = hashself
   cls = FrozenType(name, bases, dct)
   return cls(anobj)

def test():
   class bob:
     def doit(self): print 1,2,3,4
   # amutable = bob()
   # amutable = [1,2,3,4]
   amutable = set((1,2,3,4))
   # amutable = {1:2, 3:4}
   frozen = freeze(amutable)
   print frozen
   print type(frozen)
   adict = {frozen:100}
   frozen2 = freeze(amutable)
   print adict[frozen2]
   print frozen is frozen2
   print frozen == frozen2


More information about the Python-list mailing list