[Python] 使用*args與**kwargs 傳遞多參數

*args

用作傳入多個參數

def test(var1,var2,*args):
    print var1
    print var2
    print args
    print type(args)
    
test('var1','var2','var3','var4','var5')

用作call function

def test(var1,var2,var3):
    print 'Calling function with *args'
    print var1
    print var2
    print var3

vs = ('v1','v2','v3')    
test(*vs)

**kwargs

用作傳入多個參數,傳入類型為dict (key=value)

def test(**kwargs):
    print type(kwargs)
    for k,v in kwargs.items():
        print k,v
    
test(k1='v1',k2='v2')

用作call function,傳入類型為dict ( {function參數名:值} )

def test(var1,var2,var3):
    print 'Calling function with **kwargs'
    print var1
    print var2
    print var3

d = {'var1':'v1','var2':'v2','var3':'v3'}
test(**d)