1 # from http://code.activestate.com/recipes/523034/
3 from collections import defaultdict
5 class defaultdict(dict):
6 def __init__(self, default_factory=None, *a, **kw):
7 if (default_factory is not None and
8 not hasattr(default_factory, '__call__')):
9 raise TypeError('first argument must be callable')
10 dict.__init__(self, *a, **kw)
11 self.default_factory = default_factory
12 def __getitem__(self, key):
14 return dict.__getitem__(self, key)
16 return self.__missing__(key)
17 def __missing__(self, key):
18 if self.default_factory is None:
20 self[key] = value = self.default_factory()
23 if self.default_factory is None:
26 args = self.default_factory,
27 return type(self), args, None, None, self.items()
29 return self.__copy__()
31 return type(self)(self.default_factory, self)
32 def __deepcopy__(self, memo):
34 return type(self)(self.default_factory,
35 copy.deepcopy(self.items()))
37 return 'defaultdict(%s, %s)' % (self.default_factory,