0

我正在尝试在 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})

你会怎么做?我想内联,而不是先把东西放在列表中。

4

1 回答 1

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
>>>
于 2012-10-02T11:45:26.340 回答