Python中,(*)会把接收到的参数形成一个元组,而(**)则会把接收到的参数存入一个字典
foo方法可以接收任意长度的参数,并把它们存入一个元组中
>>> def foo(*args):
print(args)
>>> foo("fruit", "animal", "human")
('fruit', 'animal', 'human')
>>> foo(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
>>> arr = [1, 2, 3, 4, 5]
# 如果我们希望将一个数组形成元组,需要在传入参数的前面 加上一个*
>>> foo(arr)
([1, 2, 3, 4, 5]
>>> foo(*arr)
(1, 2, 3, 4, 5)
(**)将接收到的参数存入一个字典
>>> def foo(**kwargs):
for key, value in kwargs.items():
print("%s=%s" % (key, value))
>>> foo(a=1, b=2, c=3)
a=1
b=2
c=3