Defaultdict with default custom class
from collection import defaultdict
class Number(object):
def __init__(self, N):
self.N = N
def __repr__(self):
return str(self.N)
d = defaultdict(Number)
d['foo']
# TypeError: __init__() takes exactly 2 arguments (1 given)
d = defaultdict(lambda: Number(10))
d['foo']
10
# OR
from functools.partial
d = defaultdict(partial(Number, 10))
d['foo']
10