2017-06-02 72 views
3

我试图做类似下面的操作:将多个参数传递给一个函数?

a = 2 
b = 4 
c = 6 

def my_function(a,b,c): 
    a1 = a*2 
    b1 = b*2 
    c1 = c*2 
    return (a1,b1,c1) 

x = my_function(a,b,c) 

my_function(x) 

在哪里,我想反复调用my_function()自身的输出是否符合一定的条件。

不过,我得到的结果:

TypeError: my_function() missing 2 required positional arguments: 'b' and 'c' 

我相信这是一个基本的问题,并已在别处回答,但它似乎很简单,这是很难寻找。

+1

使用'my_function(* x)'使'x'-tuple *爆炸*并创建与它包含的元素一样多的输入。 –

+7

'my_function(* x)' - 见https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists – khelwood

回答

2

使用“星号”语法。

my_function(*x) 

有关更多信息,请参见this post

+0

完美,谢谢。我知道这会很简单! –

4

使用*解包元组,以便它的所有元素都可以作为不同的参数传递。

my_function(*x)