是的,这是我应该说的。
def foo(*args):
return bar(*args)
您不需要用(a,b,c)声明函数。酒吧(...)将获得任何foo(...)获得。
我的其他crummier的回答是以下:
我是如此接近回答“不,这不容易做到”,但有一些多余的线条,我觉得还可以。 @cbrauchli伟大的想法用当地人(),但由于当地人()也返回局部变量,如果我们做
def foo(a,b,c):
n = "foobar" # any code that declares local variables will affect locals()
return bar(**locals())
我们将传递一个不想要的第四个参数,N,吧(A,B,C ),我们会得到一个错误。为了解决这个问题,你需要在第一行做一些类似于arguments = locals()的事情,例如:
def foo(a, b, c):
myargs = locals() # at this point, locals only has a,b,c
total = a + b + C# we can do what we like until the end
return bar(**myargs) # turn the dictionary of a,b,c into a keyword list using **
当@ andrew的一个更简单,更多Pythonic并且直截了当时,这个答案是如何被接受的? – ereOn 2014-05-22 06:10:32
我真的很惊讶自己。 – pagga 2015-03-10 06:02:19