相关疑难解决方法(0)

可以部分应用不带关键字参数的函数的第二个参数吗?

以python内置pow()函数为例.

xs = [1,2,3,4,5,6,7,8]

from functools import partial

list(map(partial(pow,2),xs))

>>> [2, 4, 8, 16, 32, 128, 256]
Run Code Online (Sandbox Code Playgroud)

但是我如何将xs提高到2的幂?

要得到 [1, 4, 9, 16, 25, 49, 64]

list(map(partial(pow,y=2),xs))

TypeError: pow() takes no keyword arguments
Run Code Online (Sandbox Code Playgroud)

我知道列表理解会更容易.

python arguments partial-application

38
推荐指数
5
解决办法
1万
查看次数

标签 统计

arguments ×1

partial-application ×1

python ×1