Python &引用;TypeError:foo()只接受1个参数;使用kwargs执行Pool.map()时

Python &引用;TypeError:foo()只接受1个参数;使用kwargs执行Pool.map()时,python,Python,我正在为带有**kwargs的函数创建functools.partial from functools import partial def foo(required, **kwargs): return required + str(kwargs) _foo = partial(foo, "hello", bar='baz') foo(“hello”,bar='baz')和\u foo()都打印预期输出: In [4]: foo("hello", bar="baz") Out[4

我正在为带有
**kwargs
的函数创建
functools.partial

from functools import partial

def foo(required, **kwargs):
    return required + str(kwargs)

_foo = partial(foo, "hello", bar='baz')
foo(“hello”,bar='baz')
\u foo()
都打印预期输出:

In [4]: foo("hello", bar="baz")
Out[4]: "hello{'bar': 'baz'}"
In [5]: _foo()
Out[5]: "hello{'bar': 'baz'}"
我尝试将此
partial
作为
多处理.Pool
的一部分运行:

import multiprocessing as mp
pool = mp.Pool()
results = pool.map(_foo, range(2)) # Run the _foo partial twice
但我得到:

TypeError: foo() takes exactly 1 argument (3 given)

如何在池中执行
foo
,并提供所需的关键字参数?

首先绑定位置参数
required
,然后在
map
调用中隐式传递另一个位置参数(第一次调用0,第二次调用1)

当然,这些都是无效调用,无论使用何种池,都很容易演示:

_foo = partial(foo, "hello", bar='baz')  # required is bound to "hello" here
_foo(0)  # no more position arguments are allowed...
=> TypeError: foo() takes exactly 1 argument (3 given)

您希望对
\u foo
进行什么调用?它正在调用
\u foo(0)
\u foo(1)
,这很容易验证它们是否无效,而与multiprocessing.Pool.doh无关!我忘记了,
Pool.map
调用将第二个参数放入函数中!把这个写下来作为答案,我会接受的!如果调用
\u foo(0)
\u foo(1)
时,错误不是说
TypeError:foo()接受1个位置参数,但2个位置参数被赋予了
,而不是3个位置参数吗?没关系,显然Python 3 TypeErrors比Python 2 TypeErrors提供了更具体的信息。我想我忽略了这一点,因为在我的情况下,实际上我并不关心迭代次数,
[0,1]
,所以我忽略了它们。