1 try:
2 import collections
3 defaultdict = collections.defaultdict
4 except AttributeError:
5 print '*Warning*: Using local class definition of defaultdict'
6 print ' collections.defaultdict not available until Python 2.5'
8 """Subclass of dict which sets key to a default value
9 when key is unknown. defaultvalue supplied by callable
10 default_factory supplied when dict instance is created.
11 default_factory must be callable with no args."""
12 - def __init__(self,default_factory, *a, **k):
13 dict.__init__(self, *a, **k)
14 self.default_factory = default_factory
15
17 if key not in self and self.default_factory is not None:
18 self[key] = self.default_factory()
19 return dict.__getitem__(self,key)
20
22
24 defaultdict.__init__(self,list,*a,**k)
25