上一段代碼,大家感受一下
def test_param(*args):
print(args)
def test_param2(**args):
print(args)
test_param('test1','test2')
>>>('test1',test2')
test_param2(p1='test1',p2='test2')
>>>{'p1':'test1', 'p2':'test2'}
python提供了兩種特別的方法來定義函數的參數:
1. 位置參數 *args,??把參數收集到一個元組中,作為變量args
??def show_args(*args):? ?=>??how_args("hello", "world")
2. 關鍵字參數 **kwargs, 是一個正常的python字典類型,包含參數名和值
??def show_kwargs(**args):??= > show_kwargs(foo="bar", spam="eggs")