The examples given in the docs all seem to be the cases where the first callable argument in defaultdict
is a "constant" function, like int
, list
, or lambda: const
etc. I don't know if defaultdict
is just supposed to take constant functions as its callabe argument, but if not, I want the callable to be dependent on the missing key values I give. For example
toy = collections.defaultdict(lambda x: len(x), {'foo': 3})
toy['barr']
What I expect is whenever I give a missing key string, for example 'barr'
, the container can create a new entry with 'barr'
being the key and its length 4
being the value. But it doesn't work. Instead, it gives me the following error:
TypeError: <lambda>() missing 1 required positional argument: 'x'
I expected 'barr'
to be this lambda
's argument here, but it was apparently not the case. So what could have gone wrong?
The default factory is never given any arguments, so you can't do it like that. What you can do, though, is subclass defaultdict
and define the __missing__
method:
class CustomDefaultDict(collections.defaultdict):
def __missing__(self, key):
self[key] = value = len(key)
return value
To make it more easily-extendible, you could also take advantage of the default_factory
, but give it the key as an argument:
class CustomDefaultDict(collections.defaultdict):
def __missing__(self, key):
if self.default_factory is None:
raise KeyError((key,))
self[key] = value = self.default_factory(key)
return value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With