将参数列表传递给Python函数

这个问题在这里已经有了答案:

  • **(双星/星号)和*(星号/星号)对参数做什么? 15个答案

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

    这相当于:

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

    固定的x参数可能需要考虑:

    func(5, *some_list)
    

    ...相当于:

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

    如果你没有指定x值(在上面的例子中是5 ),那么some_list第一个值将作为x参数传递给func


    将值传递为逗号分隔值

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

    您可以通过阅读文档了解更多信息

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

    上一篇: Passing a list of parameters into a Python function

    下一篇: what does double star followed by variable name mean in python?