Python 2.5并在函数参数中列出

时间:2012-10-02 11:03:09

标签: python python-2.5

我正在尝试在Python2.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()

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 个答案:

答案 0 :(得分: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
>>>