1. 程式人生 > >python3裏函數怎麽樣使用元組或字典作為參數調用(復制他人博客)

python3裏函數怎麽樣使用元組或字典作為參數調用(復制他人博客)

元組 bsp 字典 復制 參數調用 博客 The 參數 app

在python3中可以采用如下方法:

函數(*(元組))

函數(**{字典})

如下例子:
function(*("whither", "canada?")) 元組
function(*(1, 2 + 3)) 元組
function(**{"a": "crunchy", "b": "frog"}) 字典

在python2中,可以使用apply來實現

def function(a, b):
print a, b

apply(function, ("whither", "canada?"))
apply(function, (1, 2 + 3))

python3裏函數怎麽樣使用元組或字典作為參數調用(復制他人博客)