*args和**kwargs是什么意思?
def foo(x, y, *args):
def bar(x, y, **kwargs):
*args和**kwargs是什么意思?
def foo(x, y, *args):
def bar(x, y, **kwargs):
当前回答
此表便于在函数构造和函数调用中使用*和**:
In function construction In function call
=======================================================================
| def f(*args): | def f(a, b):
*args | for arg in args: | return a + b
| print(arg) | args = (1, 2)
| f(1, 2) | f(*args)
----------|--------------------------------|---------------------------
| def f(a, b): | def f(a, b):
**kwargs | return a + b | return a + b
| def g(**kwargs): | kwargs = dict(a=1, b=2)
| return f(**kwargs) | f(**kwargs)
| g(a=1, b=2) |
-----------------------------------------------------------------------
这真的只是用来概括洛林·霍希斯坦的答案,但我觉得它很有用。
相关地:在Python 3中扩展了星形/splat运算符的用法
其他回答
TL;博士
它将传递给函数的参数分别打包到函数体中的list和dict中。当您这样定义函数签名时:
def func(*args, **kwds):
# do stuff
它可以用任意数量的参数和关键字参数调用。非关键字参数被打包到函数体内名为args的列表中,关键字参数被包装到函数体内称为kwds的dict中。
func("this", "is a list of", "non-keyowrd", "arguments", keyword="ligma", options=[1,2,3])
现在在函数体内部,当调用函数时,有两个局部变量,args是一个值为[“this”,“is a list of”,“non-keyword”,“arguments”]的列表,kwds是一个具有值为{“keyword”:“ligma”,“options”:[1,2]}的dict
这也反过来起作用,即从呼叫者一侧起。例如,如果函数定义为:
def f(a, b, c, d=1, e=10):
# do stuff
您可以通过打开调用范围中的可迭代项或映射来调用它:
iterable = [1, 20, 500]
mapping = {"d" : 100, "e": 3}
f(*iterable, **mapping)
# That call is equivalent to
f(1, 20, 500, d=100, e=3)
def foo(param1,*param2):是一个可以接受任意数量的*param2值的方法,def bar(param1,**param2):是一种方法,可以接受任意数量的带有*param2键的值param1是一个简单的参数。
例如,在Java中实现varargs的语法如下:
accessModifier methodName(datatype… arg) {
// method body
}
*args(或*any)表示每个参数
def any_param(*param):
pass
any_param(1)
any_param(1,1)
any_param(1,1,1)
any_param(1,...)
注意:不能将参数传递给*args
def any_param(*param):
pass
any_param() # will work correct
*参数的类型为元组
def any_param(*param):
return type(param)
any_param(1) #tuple
any_param() # tuple
用于访问不使用的元素*
def any(*param):
param[0] # correct
def any(*param):
*param[0] # incorrect
**千瓦时
**kwd或**任何这是字典类型
def func(**any):
return type(any) # dict
def func(**any):
return any
func(width="10",height="20") # {width="10",height="20")
单*表示可以有任意数量的额外位置参数。foo()可以像foo(1,2,3,4,5)一样调用。在foo()的主体中,param2是一个包含2-5的序列。
双**表示可以有任意数量的额外命名参数。bar()可以像bar(1,a=2,b=3)一样调用。在bar()的主体中,param2是一个字典,其中包含{‘a‘:2,‘b‘:3}
使用以下代码:
def foo(param1, *param2):
print(param1)
print(param2)
def bar(param1, **param2):
print(param1)
print(param2)
foo(1,2,3,4,5)
bar(1,a=2,b=3)
输出为
1
(2, 3, 4, 5)
1
{'a': 2, 'b': 3}
在Python 3.5中,您还可以在列表、字典、元组和集合显示(有时也称为文字)中使用此语法。参见PEP 488:其他解包概括。
>>> (0, *range(1, 4), 5, *range(6, 8))
(0, 1, 2, 3, 5, 6, 7)
>>> [0, *range(1, 4), 5, *range(6, 8)]
[0, 1, 2, 3, 5, 6, 7]
>>> {0, *range(1, 4), 5, *range(6, 8)}
{0, 1, 2, 3, 5, 6, 7}
>>> d = {'one': 1, 'two': 2, 'three': 3}
>>> e = {'six': 6, 'seven': 7}
>>> {'zero': 0, **d, 'five': 5, **e}
{'five': 5, 'seven': 7, 'two': 2, 'one': 1, 'three': 3, 'six': 6, 'zero': 0}
它还允许在单个函数调用中解包多个可迭代项。
>>> range(*[1, 10], *[2])
range(1, 10, 2)
(感谢mgilson提供PEP链接。)