参数组:
def test(*args): print(args) test(1,2,3,4,5,6) test(*[1,2,3])#转成元祖 def test1(a,*args): #args只能接收位置参数,不能接受关键字参数 print(a) print(args) test1(1,2,3,4,5) #1会传给a,2345传给args def test2(**kwargs): #kwargs接收关键字参数,转成字典 print(kwargs) print(kwargs[‘name‘]) print(kwargs[‘age‘]) test2(name=‘deakin‘,age=28)#字典 test2(**{‘name‘:‘deakin‘,‘age‘:28}) def test3(name,**kwargs): print(name) print(kwargs) test3(‘john‘,age=10,sex=‘M‘) def test4(name,age=18,**kwargs): print(name) print(age) print(kwargs) test4(‘deakin‘,job=‘IT‘) def test5(name,age=28,*args,**kwargs): print(name) print(age) print(args) print(kwargs) test5(‘amy‘,20,2,3,4,5,‘deakin‘,job=‘IT‘) #2345,deakin都是位置参数,传给args转为元祖 打印结果: test: (1, 2, 3, 4, 5, 6) (1, 2, 3) test1:
1 (2, 3, 4, 5) test2: {‘age‘: 28, ‘name‘: ‘deakin‘} deakin 28 {‘age‘: 28, ‘name‘: ‘deakin‘} deakin 28 test3: john {‘sex‘: ‘M‘, ‘age‘: 10} test4: deakin 18 {‘job‘: ‘IT‘} test5: amy 20 (2, 3, 4, 5, ‘deakin‘) {‘job‘: ‘IT‘}