I get some strange behaviour of colletions.defaultdict
:
import collections
c1 = collections.defaultdict(str)
c1['new'] # Works!
c2 = collections.defaultdict(default_factory=str)
c2['new'] # Raises KeyError...
Why raises c2
a KeyError?
Sometimes I like naming parameter because I think it increases readability.
First I thought maybe python does not allow me to pass the parameter by naming it and puts my default_factory
parameter to the kwargs, so I checked:
def func(first, **kwargs):
print(first)
print(kwargs)
func(first='one', second='two')
This outputs:
one
{'second': 'two'}
So this is not the case.