3 class frozendict(dict):
4 def _blocked_attribute(obj):
5 raise AttributeError, "A frozendict cannot be modified."
6 _blocked_attribute = property(_blocked_attribute)
8 __delitem__ = __setitem__ = clear = _blocked_attribute
9 pop = popitem = setdefault = update = _blocked_attribute
11 def __new__(cls, *args, **kw):
12 new = dict.__new__(cls)
16 if isinstance(arg, dict):
18 for k, v in arg.items():
19 if isinstance(v, dict):
20 arg[k] = frozendict(v)
21 elif isinstance(v, list):
24 if isinstance(elm, dict):
25 v_.append( frozendict(elm) )
33 dict.__init__(new, *args_, **kw)
36 def __init__(self, *args, **kw):
41 return self._cached_hash
42 except AttributeError:
43 h = self._cached_hash = hash(tuple(sorted(self.items())))
47 return "frozendict(%s)" % dict.__repr__(self)