在Python的在形參前加'*'和'**'表示動態形參
在形參前加'*'表示可以接受多個實參值存進數組
def F(a, *b) print(a) print(b) F(1, 2, 3) ''' 1 (2, 3) '''
對於在形參前加'**'表示表示接受參數轉化為字典類型
def F(**a) print(a) F(x=1, y=2) #{'x': 1, 'y': 2}
混合運用
def F(a, *b, **c) print(a) print(b) print(c) F(1, 2, 3, x=4, y=5) ''' 1 (2, 3) {'x': 4, 'y': 5} '''
def F(*a) print(a) ls = [1, 2, 3] F(ls) #表示列表作為一個元素傳入 F(*ls) #表示列表元素作為多個元素傳入 ''' ([1, 2, 3],) (1, 2, 3) '''
def F(**a) print(a) dt = dict(x=1, y=2) F(x=1, y=2) F(**dt) #作為字典傳入 ''' {'x': 1, 'y':2} {'x': 1, 'y':2} 函數調用時 dt = dict(color='red', fontproperties='SimHei') plt.plot(**dt) 等價於 plt.plot(color='red', fontproperties='SimHei') '''