参数组:*args,**kwargs
实参有按位置和按关键字两种形式定义,*args和**kwargs
- 位置参数:按照从左到右的顺序定义的参数 位置形参:必选参数 位置实参:按照位置给形参传值
-
关键字参数:按照key=value的形式定义的实参 无需按照位置为形参传值 注意的问题: 1. 关键字实参必须在位置实参右面 2. 对同一个形参不能重复传值
- 加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数
# 可写函数说明 def printinfo( arg1, *vartuple ): "打印任何传入的参数" print ("输出: ") print (arg1) print (vartuple) # 调用printinfo 函数 printinfo( 70, 60, 50 )
result
输出: 70 (60, 50)
- 加了两个星号 ** 的参数会以字典的形式导入
# 可写函数说明 def printinfo( arg1, **vardict ): "打印任何传入的参数" print ("输出: ") print (arg1) print (vardict) # 调用printinfo 函数 printinfo(1, a=2,b=3) #一个参数不能赋两个值
result
1 {'a': 2, 'b': 3}