使用元组作为函数的参数(可变参数)
>>> def func(a,b,*arg): # *星表示可变参数
... print(type(arg))
... for i in arg:
... print(i)
...
>>> func(1,2)
<class 'tuple'>
>>> func(1,2,3)
<class 'tuple'>
3
>>> func(1,2,3,4)
<class 'tuple'>
3
4
>>>
字典
练习:写一个函数,把所有的参数作为乘积,得出结果(可变参数)
>>> def func(a,b,*arg,**kw):
... result=a*b
... for i in arg:
... result*=i
... for j in kw.values():
... result*=j
... return result
...
>>> func(1,2,3,4,c=5,d=6)
720
#1,2对应a和b,3,4对应arg,c=5,d=6对应kw,因为kw必须要有两个值,key和value
>>> func(1,2,*(3,4),**{"c":5,"d":6})
720
#称之为解包