Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用未知数量的参数调用python函数_Python_Arguments - Fatal编程技术网

使用未知数量的参数调用python函数

使用未知数量的参数调用python函数,python,arguments,Python,Arguments,我正在编写一个小脚本,在其中我称为itertools.product,如下所示: for p in product(list1,list2,list3): self.createFile(p) 有没有一种方法可以让我在不事先知道要包含多少列表的情况下调用此函数 谢谢您可以使用星号或splat运算符(它有几个名称):表示产品(*列表)中的p其中列表是您要传递的元组或事物列表 def func(a,b): print (a,b) args=(1,2) func(

我正在编写一个小脚本,在其中我称为itertools.product,如下所示:

for p in  product(list1,list2,list3):
            self.createFile(p)
有没有一种方法可以让我在不事先知道要包含多少列表的情况下调用此函数


谢谢

您可以使用星号或splat运算符(它有几个名称):
表示产品(*列表)中的p
其中
列表
是您要传递的元组或事物列表

def func(a,b):
    print (a,b)

args=(1,2)
func(*args)
在定义函数以允许其接受可变数量的参数时,可以执行类似的操作:

def func2(*args): #unpacking
   print(args)  #args is a tuple

func2(1,2)  #prints (1, 2)
args = (1,2,3)
func2(*args) #prints (1, 2, 3)
当然,您可以将splat运算符与数量可变的参数组合在一起:

def func2(*args): #unpacking
   print(args)  #args is a tuple

func2(1,2)  #prints (1, 2)
args = (1,2,3)
func2(*args) #prints (1, 2, 3)
使用splat运算符(
*
)传递和收集未知数量的参数位置参数

def func(*args):
   pass

lis = [1,2,3,4,5]
func(*lis)

这与如何“用Python解压列表”不同。解开清单是这个问题的答案,而不是问题的答案。