I want to define a defaultdict
, so that if the key is missing, its value should be f'You can set {key} to inject configuration here
Consider the following code
from collections import defaultdict
# Valid, but the default value for all key will be empty string.
d1 = defaultdict(str)
d1['post_simulation'] # return ''
# This is what I intend to do,
# but it is invalid as the default_factory won't take any arguments.
d2 = defaultdict(lambda k: f'#You can inject text here by setting {k}')
d2['post_simulation']
# TypeError: <lambda>() missing 1 required positional argument: 'k'
Is there any tech that I can do this in Python by using defaultdict or other data structure?