QuTiP的函数^{
def testFunc1(a, b):
return a, b
from qutip import parallel_map
parallel_map(testFunc1, (1, 2, 3), task_args=(4,))
这将返回[(1, 4), (2, 4), (3, 4)]
。现在我想知道是否也可以为a
设置一个固定值,为b
设置一个元组。根据documentationtask_args
也可以是字典,所以我试了一下
parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4})
parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4, 'b': (1, 2, 3)})
但这会导致TypeError: can only concatenate tuple (not "dict") to tuple
当我尝试
parallel_map(testFunc1, b=(1, 2, 3), task_args={'a': 4})
我得到TypeError: parallel_map() missing 1 required positional argument: 'values'
有人知道如何将parallel_map
用于第n个位置参数(而不为每个n编写函数包装器)吗
避免在不存在问题的地方产生问题,并将
n
-ed函数调用签名中第def
-th个外部填充的iterable直接放入parallel_map()
-expected(如文档所示)中,并与迭代处理兼容tuple
:不,(s)的清晰路径,如上所述
该示例是一条通向卸载
a
和任何及所有n-th用户端代码输入指路,打电话给:
交付>
完全满足a)您对任何第N个表的自由手的愿望,不仅是第一个位置表,还有b)这正是^{} 的调用签名所期望的,并且有文件记录在案:
查看source code of ^{} 可以揭示为什么它只适用于函数的第一个参数:
在这一行中创建了并行进程。函数
task
获取一个表示所有位置参数的元组,该元组由values
的1个元素和所有其他task_args
元素创建。由于values
的元素位于组合元组((value,) + task_args
)的第一个位置,因此它始终是函数的第一个参数,在其并行实例之间变化。如果字典用于task_args
,则TypeError: can only concatenate tuple (not "dict") to tuple
的原因是+
-运算符只重载了(tuple, tuple)
,而不是重载了(tuple, dict)
因此,最终无法构建包装器。每个特定的n都有一个,或者是一个通用的,比如:
打电话给
返回
相关问题 更多 >
编程相关推荐