蟒蛇调用函数的参数数目未知 [英] Calling python function with an unknown number of arguments
本文介绍了蟒蛇调用函数的参数数目未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写一个小脚本中,我称之为itertools.product像这样:
I am writing a little script in which I call itertools.product like so:
for p in product(list1,list2,list3):
self.createFile(p)
有我调用此函数没有预先知道许多名单如何将?
Is there a way for me to call this function without knowing in advance how many lists to include?
感谢
推荐答案
您可以使用星型或图示运算符(它有几个名字):在产品为P(*列表)
,其中列表
是要传递一个事物的元组或列表。
You can use the star or splat operator (it has a few names): for p in product(*lists)
where lists
is a tuple or list of things you want to pass.
def func(a,b):
print (a,b)
args=(1,2)
func(*args)
定义一个函数时,允许它接受可变数量的参数,你可以做类似的事情:
You can do a similar thing when defining a function to allow it to accept a variable number of arguments:
def func2(*args): #unpacking
print(args) #args is a tuple
func2(1,2) #prints (1, 2)
当然,你可以用的可变参数的数量结合图示运营商:
And of course, you can combine the splat operator with the variable number of arguments:
args = (1,2,3)
func2(*args) #prints (1, 2, 3)
这篇关于蟒蛇调用函数的参数数目未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文