可变长参数分为非关键字可变长参数和关键字可变长参数两种。
- 非关键字可变长参数——语法为
Def func(*tuple)
,tuple为元组。 - 关键字可变长参数——语法为
Def func(**dict)
,dict为字典。
非关键字可变长参数示例。
def test(*animal):
print('常见的动物有:',animal)
test('monkey','pig','bird')
运行结果如下。
常见的动物有: (‘monkey’, ‘pig’, ‘bird’)
关键字可变长参数示例。
def test(**animal):
print('常见的动物有:',animal)
test(monkey='猴子',pig='猪',bird='鸟')
常见的动物有: {‘monkey’: ‘猴子’, ‘pig’: ‘猪’, ‘bird’: ‘鸟’}