我想我会使用这个简单的一行代码:
import itertools
print list(itertools.imap(pow, [1, 2, 3], itertools.repeat(2)))
更新:
我还想到了一个更有趣但没什么用的解决方案。它是一种美丽的语法糖,利用Python3中...
字面量表示省略号
的特性。这是partial
的修改版本,允许省略左右两侧之间的某些位置参数。唯一的缺点是你不能再将省略号作为参数传递。
import itertools
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(newfunc.leftmost_args + fargs + newfunc.rightmost_args), **newkeywords)
newfunc.func = func
args = iter(args)
newfunc.leftmost_args = tuple(itertools.takewhile(lambda v: v != Ellipsis, args))
newfunc.rightmost_args = tuple(args)
newfunc.keywords = keywords
return newfunc
>>> print partial(pow, ..., 2, 3)(5)
1
>>> print partial(pow, 2, ..., 3)(5)
2
>>> print partial(pow, 2, 3, ...)(5)
3
>>> print partial(pow, 2, 3)(5)
3
因此,对于原问题的解决方案将是使用这个版本的partial:list(map(partial(pow, ..., 2),xs))
def meth(cls, self,...)
,然后使用partial(meth, cls)
。 - nadapez