4 ms·
I think they're pointing out that the callable should be given the key value. So like, let's say a missing key should be assigned its own value by default. Belo
by BWStearns 5y ago
I think they're pointing out that the callable should be given the key value. So like, let's say a missing key should be assigned its own value by default. Below I try to do that but the callable doesn't seem to be passed the key.
```
In [22]: foo = defaultdict(lambda k: k)
In [23]: foo[1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-5a39798fc62e> in <module>
----> 1 foo[1]
TypeError: <lambda>() missing 1 required positional argument: 'k'
```
I could envision this being useful for using a defaultdict as something like a local cache for something where requests are expensive. Below is how one might want to use it but it'll fail because the lambda is not given the key argument.
```
profiles = defaultdict(lambda p_id: get_profile_from_site(p_id))
```