2012-10-02 41 views
0

我想在的python2.5的Python 2.5和列表函數參數

有所建樹

所以,我有我的功能

def f(a,b,c,d,e): 
    pass 

,現在我想調用該函數:(以python2.7我會做)

my_tuple = (1,2,3) 
f(0, *my_tuple, e=4) 

但是在python2.5中沒有辦法做到這一點。我在考慮申請()

apply(f, something magical here) 

#this doesn't work - multiple value for 'a'. But it's the only thing I came up with 
apply(f, my_tuple, {"a":0, "e":4}) 

你會怎麼做?我希望將它內聯,而不要將事情放在列表中。

回答

1

如果你願意調劑的參數的順序,那麼你可以使用這樣的事情:

>>> def f(a,b,c,d,e): 
... print a,b,c,d,e 
... 
>>> my_tuple = (1,2,3) 
>>> def apply(f, mid, *args, **kwargs): 
... return f(*args+mid, **kwargs) 
... 
>>> apply(f, my_tuple, 0, e=4) 
0 1 2 3 4 
>>> 
>>> apply(f, ('a', 'b'), '_', d='c', e='d') 
_ a b c d 
>>>