Python *args **kwargs 函数传参
我们写程序时,不确定要往函数中传入多少个参数,即可使用可变参数(不定长参数),用 *args
,**kwargs
表示。
*args
被称为 Non-keyword Variable Arguments,无关键字参数。
**kwargs
被称为 Keyword Variable Arguments,有关键字参数。
*args
以列表或者元组形式传参,**kwargs
以字典形式传参。
*args
实例:
当位置参数和不定长参数一起使用时,先把参数分配给位置参数,再将多余的参数以元组形式分配给 args
。
1 |
|
**kwargs
实例:
当传入参数为字典时,使用 **kwargs
。
1 |
|
Python *args **kwargs 函数传参
https://pandintelli.github.io/2022/01/17/Python-Variable-Length-Parameters/