kwargs.pop

pop(key[, default]) if key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

kwargs.pop()的作用是将字典中指定的键移除,并返回其对应的值。如果字典中没有指定的键,则返回默认值,没有设置默认值的话就报错。

1
2
3
4
d = {'a' :1, 'c' :2}
print(d.pop('c', 0)) # return 2
print(d) # returns {'a': 1}
print(d.get('c', 0)) # return 0

参考

https://www.cnpython.com/qa/50845


kwargs.pop
http://yojayc.github.io/2021/06/14/kwargs-pop/
作者
Truman
发布于
2021年6月14日
更新于
2026年4月5日
许可协议