开发者

Python: some function for args -> *args (similar like those in functools)

I had these implementations:

def vecAdd(v1, v2): return tuple(map(add,开发者_如何学运维 izip(v1,v2)))
def vecMul(v1, f): return tuple(map(mul, izip(v1,repeat(f))))

Those didn't work because add (and mul) is called like add((x,y)), i.e. it only gets one single argument.

Is there some function which basically does the following?

def funccaller_with_exposed_args(func):
    return lambda args: func(*args)

Maybe this is overkill and overengineered in this case but in general, this can be very important performance wise if you otherwise could lay out a complete heavy loop into pure C code.


You could do this with itertools.starmap or itertools.imap. imap is like starmap except that it zips the arguments first. So instead of calling izip yourself, you could just use imap:

import itertools as it
def vecAdd(v1, v2): return tuple(it.imap(add, v1, v2))
def vecMul(v1, f): return tuple(it.imap(mul, v1, it.repeat(f)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜