A class has a constructor which takes one parameter:
class C(object):
def __init__(self, v):
self.v = v
...
Somewhere in the code, it is useful for values in a dict to know their keys.
I want to use a defaultdict with the key passed to newborn default values:
d = defaultdict(lambda : C(here_i_wish_the_key_to_be))
Any suggestions?
It hardly qualifies as clever - but subclassing is your friend:
class keydefaultdict(defaultdict):
def __missing__(self, key):
if self.default_factory is None:
raise KeyError( key )
else:
ret = self[key] = self.default_factory(key)
return ret
d = keydefaultdict(C)
d[x] # returns C(x)