天天看點

Python函數中可變參數和強制命名參數

一、可變參數

1.元組

"""可變參數"""

def test01(a,b,*c):

    print(a,b,c)

test01(10,20,33,44,55)    # 10 20 (33, 44, 55)      

2.字典

def test02(a,b,**c):

    print(a,b,c)


test02(1,2,name="chu",age=18,job="teacher")    # 1 2 {'name': 'chu', 'age': 18, 'job': 'teacher'}      

3.萬能參數

def test03(a,b,*c,**d):

    print(a,b,c,d)


test03(111,222,33,44,55,name="baobao",age=18) # 111 222 (33, 44, 55) {'name': 'baobao', 'age': 18}      

二、強制命名參數

"""強制命名參數"""

def test04(*a,b,c):
    print(a,b,c)


#test04(666,777,88,99)   # test04() missing 2 required keyword-only arguments: 'b' and 'c'
test04(666,777,b=12,c=13)  # (666, 777) 12 13