Python - use list as function parameters

本小妞迷上赌 提交于 2019-11-26 02:32:48

问题


How can I use a Python list (e.g. params = [\'a\',3.4,None]) as parameters to a function, e.g.:

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

回答1:


You can do this using the splat operator:

some_func(*params)

This causes the function to receive each list item as a separate parameter. There's a description here: http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists




回答2:


This has already been answered perfectly, but since I just came to this page and did not understand immediately I am just going to add a simple but complete example.

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

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

>> a



回答3:


Use an asterisk:

some_func(*params)



回答4:


You want the argument unpacking operator *.



来源:https://stackoverflow.com/questions/4979542/python-use-list-as-function-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!