def fun(a, b, *args): print(a) print(b) print(args) print("="*30) ret = a + b for i in args: ret += i return ret print(fun(1,2,3,4)) 结果: 1 2 (3, 4) ============================== 10
1,2分别赋值给a,b,剩下的参数以元组的形式赋值给args
字典形式参数:
def fun(a, b, *args, **kwargs): print(a) print(b) print(args) print(kwargs) fun(1, 2, 3, 4, name = "hello", age = 20) 结果: 1 2 (3, 4) {‘name‘: ‘hello‘, ‘age‘: 20}
传入元组和字典:
def fun(a, b, *args, **kwargs): print(a) print(b) print(args) print(kwargs) tup = (11,22,33) dic = {"name":"hello", "age":20} fun(1, 2, *tup, **dic) 结果: 1 2 (11, 22, 33) {‘name‘: ‘hello‘, ‘age‘: 20}