[英]multiple values for keyword argument when using **kwargs
Function _mget_search definition will be as, 函数_mget_search的定义将为
def _mget_search(query, with_scores=False, count=False, record_term=True,
**kwargs):
This function called with keyword arguments. 使用关键字参数调用此函数。
_mget_search(**search_kwargs)
Function parameter search_kwargs contains as, 函数参数search_kwargs包含为,
{'count': False, 'min_score': 20, 'end': 49, 'with_scores': False, 'start': 0, 'query': u'ouuuuu', 'max_score': inf, 'record_term': True}
I am getting this error - 我收到此错误-
_mget_search() got multiple values for keyword argument 'query'
I am unable to understand why it is happening and how to correct it. 我不明白为什么会这样以及如何纠正它。
It's because you are unpacking arguments you are trying to pass. 这是因为您要解压缩要传递的参数。 Try to use:
尝试使用:
_mget_search(search_kwargs)
EDIT 编辑
Let's dive more into this problem. 让我们深入研究这个问题。 We'll define two functions that and see how they behave when passing various arguments.
我们将定义两个函数,并查看它们在传递各种参数时的行为。
In [1]: def fun1(a, b, c):
...: print('Passed arguments: a={} b={} c={}'.format(a, b, c))
...:
In [2]: def fun2(*args):
...: print('Passed args: {}'.format(args))
...:
In [3]: fun1(1, 2, 3)
Passed arguments: a=1 b=2 c=3
In [4]: fun2(1, 2, 3)
Passed args: (1, 2, 3)
In [5]: fun2([1, 2, 3])
Passed args: ([1, 2, 3],)
In [6]: fun2(*[1, 2, 3])
Passed args: (1, 2, 3)
In [7]: fun1(*[1, 2, 3])
Passed arguments: a=1 b=2 c=3
In 3rd call we passed 3 arguments separately which is the same as if we used 7th call where an unpacked list was called. 在第3次调用中,我们分别传递了3个参数,这与在第7次调用中调用解压缩列表的情况相同。 Compare it with situations when
fun2
was called. 将其与调用
fun2
情况进行比较。
**kwargs allows you to pass keyworded variable length of arguments to a function. ** kwargs允许您将关键字的可变参数长度传递给函数。 You should use **kwargs if you want to handle named arguments in a function.
如果要处理函数中的命名参数,则应使用** kwargs。 Here is an example to get you going with it:
这是一个示例,可以助您一臂之力:
def greet_me(**kwargs):
if kwargs is not None:
for key, value in kwargs.iteritems():
print "%s == %s" %(key,value)
>>> greet_me(name="yasoob")
name == yasoob
I would bet that something somewhere is also passing a parameter to that function, in addition to the search_kwargs
. 我敢打赌,除了
search_kwargs
之外,某处的某处还在向该函数传递参数。 If it's part of a callback function, there's a good chance the callback is calling it with some parameters and those are getting assigned to the first parameter query
. 如果它是回调函数的一部分,则很有可能该回调使用一些参数来调用它,而这些参数将被分配给第一个参数
query
。
The only way that I can see you getting that error is if you called your function with a parameter and kwargs, like so: 我看到您收到该错误的唯一方法是,如果您使用参数和 kwargs调用了函数,如下所示:
assuming: 假设:
def _mget_search(query, with_scores=False, count=False, record_term=True,
**kwargs):
then the way to get that error message is: 那么获取该错误消息的方法是:
>>> search_kwargs= {'query': u'ouuuuu', 'count': False, 'min_score': 20, 'end': 49, 'with_scores': False, 'start': 0, 'max_score': inf, 'record_term': True}
>>> _mget_search(some_random_parameter, **search_kwargs)
Here's what I used to try and recreate the error. 这是我用来尝试重新创建错误的方法。
def func(a, b=2, **kwargs):
return '{}{}{}'.format(a, b, kwargs)
Only the last one succeeded in getting that error. 只有最后一个成功获得该错误。
>>> func(**{'a':2, 'b': 3, 'other': 3})
"23{'other': 3}"
>>> func(7, **{'stuff': 5})
"72{'stuff': 5}"
>>> func(2, **{'b': 7})
'27{}'
>>> func(**{'b': 3, 'other': 3, 'a': 5})
"53{'other': 3}"
>>> func(2, **{'a': 5})
TypeError: func() got multiple values for argument 'a'
I believe the issue is because query
is defined as a positional arg, so when the dictionary you pass with ** is unpacked, the first entry (regardless of its name) is being used for query, then query also appears in the dictionary, hence the multiple values for query
. 我认为问题在于,因为
query
被定义为位置arg,所以当您通过**传递的字典被解压缩时,第一个条目(无论其名称如何)都用于查询,然后查询也出现在字典中,因此query
的多个值。
Try fixing by making query a named arg: 尝试通过将查询命名为arg来修复:
def _mget_search(query='', with_scores=False, count=False, record_term=True, **kwargs):
Alternatively, don't include query in the dictionary when you pass it. 另外,通过时不要在字典中包含查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.