2011-02-12 66 views
71

我如何使用Python的列表(例如params = ['a',3.4,None])作爲參數傳遞給函數,如:Python的 - 使用列表作爲函數參數

def some_func(a_char,a_float,a_something): 
    # do stuff 
+0

相關:[Python 3的註解:類型提示指定類型(PyCharm)的列表(https://stackoverflow.com/questions/24853923/python-3-annotations-type-hinting-a-一個指定類型的pycharm) – 2018-02-27 20:36:20

回答

13

使用星號:

some_func(*params) 
+4

沿着這些線,你也可以使用一個字典:def f(a,b,c):#do stuff。 mydict = {'a':1,'b':2,'c':3} f(** mydict) – inspectorG4dget 2011-02-12 17:47:34

42

這已經得到了很好的回答,但是由於我剛到這個頁面並且不能立即理解,我只是想添加一個簡單但完整的示例。

def some_func(a_char, a_float, a_something): 
    print a_char 

params = ['a', 3.4, None] 
some_func(*params) 

>> a