Passing a list of parameters into a Python function

This question already has an answer here:

  • What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 15 answers

  • some_list = ["some", "values", "in", "a", "list", ]
    func(*some_list)
    

    This is equivalent to:

    func("some", "values", "in", "a", "list")
    

    The fixed x param might warrant a thought:

    func(5, *some_list)
    

    ... is equivalent to:

    func(5, "some", "values", "in", "a", "list")
    

    If you don't specify value for x ( 5 in the example above), then first value of some_list will get passed to func as x param.


    Pass the values as comma separated values

    >>> def func(x, *p):           # p is stored as tuple
    ...     print "x =",x
    ...     for i in p:
    ...         print i
    ...     return p
    ... 
    >>> print func(1,2,3,4)        # x value 1, p takes the rest
    x = 1
    2
    3
    4
    (2,3,4)                        # returns p as a tuple
    

    You can learn more by reading the docs

    链接地址: http://www.djcxy.com/p/9064.html

    上一篇: python zip(* X)用“*”(星号)做什么?

    下一篇: 将参数列表传递给Python函数