1. Tuple form
def test1 (*args): Print ( ' ############### #test1 ################ ') print ( type (args)) print (args)
Call correctly:
Test1 (1, 2) #args在函数体内部为tuple类型
Error Call:
Test1 (1, b=2) #TypeError: Test1 () got an unexpected keyword argument ' b '
Test1 (A=1, b=2) #TypeError: Test1 () got an unexpected keyword argument ' a '
Test1 (A=1, 2) #TypeError: Test1 () got an unexpected keyword argument ' a '
2. Dictionary form
def test2 (**kargs): Print ( ' ############### #test2 ################ ') print ( type (Kargs)) print (Kargs)
Correct adjustment :
Test2 (A=1, b=2) #kargs在函数体内部为dict类型
Error Call:
Test2 (1, 2) #TypeError: Test2 () takes exactly 0 arguments (2 given)
Test2 (1, b=2) #TypeError: Test2 () takes exactly 0 arguments (2 given)
Test2 (A=1, 2) #SyntaxError: non-keyword arg after keyword arg
3. Mixed form
def test3 (*args, **kargs): Print ( ' ############### #test3 ################ ') print ( type (args)) print ( args) print ( type (Kargs)) print (Kargs
Call correctly:
Test3 (1, 2) #args在函数体内部为tuple类型, Kargs is an empty dict type
Test3 (1, b=2) #args在函数体内部为tuple类型, Kargs for dict type
Test3 (A=1, b=2) #args在函数体内部为空tuple类型, Kargs to Dict type
Error Call:
Test3 (A=1, 2) #SyntaxError: non-keyword arg after keyword arg
Practice:
1 defTest (num,*args,**Kwargs):2 Print('---test1---%d'%num)3 Print('---test2---', args)4 Print('---test3---', Kwargs)5 6Test (100)7Test (100,200)8Test (100,200,300,MM=100)
Answer:
1 c:\>python test.py 2 ---test1---100 3 --- Test2--- () 4 ---test3--- {} 5 6 --- Test1---100 7 ---test2---(200 8 ---test3--- {} 9 10 ---test1---10011 ---test2---( 300) 12 ---test3---{" mm ' : '
Python Indeterminate parameter function