Python's dict.pop(key[, default]) ignores the default value set by collections.defaultdict(default_factory) as shown by the following code snippet:
from collections import defaultdict
d = defaultdict(lambda: 1)
d['a']
d['b'] = 2
print(d.pop('a')) # 1
print(d.pop('b')) # 2
print(d.pop('c', 3)) # 3
d.pop('e') # KeyError: 'e'
d is a defaultdict(lambda: 1). d.pop('e') causes a KeyError. Is this intended? Shouldn't d.pop('e') return 1 since that's the default value set by defaultdict?
TL;DR: In summary, defaultDict will make
dict['inexistent-key']return your default value, anythying else should have the same behaviour as a normal dict.Explanation
The documentation you linked states that:
That is further specified under the
__missing__()method itself, which is called by__getitem__()on a dict:So not only
pop()will have the same behaviour,get()will too. The only way to have the default value would be to straight up use[key]on your dict. And if we think about it, it's definitely the most relevant call on a dict.