是的,這是我應該說的。
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