1. 程式人生 > >python3裡函式怎麼樣使用元組或字典作為引數呼叫(複製他人部落格)

python3裡函式怎麼樣使用元組或字典作為引數呼叫(複製他人部落格)

在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))