简体   繁体   English

当在python函数中使用* args,** kwargs时,“为关键字参数获取了多个值”

[英]“got multiple values for keyword argument” when using *args, **kwargs in a python function

When passing a named parameter request through **kwargs , I get an error- 通过**kwargs传递命名参数request ,我收到错误 -

Traceback (most recent call last):
  File "testKwargs.py", line 9, in <module>
    load_strategy(request="myReq", backend="myBackend", redirect_uri=None, *args, **kwargs)
  File "testKwargs.py", line 5, in load_strategy
    get_strategy("backends", "strategy", "storage", *args, **kwargs)
TypeError: get_strategy() got multiple values for keyword argument 'request'

The code in testKwargs.py is below- testKwargs.py的代码如下 -

def get_strategy(backends, strategy, storage, request=None, backend=None, *args, **kwargs):
    print request

def load_strategy(*args, **kwargs):
    get_strategy("backends", "strategy", "storage", *args, **kwargs)

args = ([],)
kwargs = {"acess_token":"myAccToken", "id":"myId"}
load_strategy(request="myReq", backend="myBackend", redirect_uri=None, *args, **kwargs)

I was expecting that there would be one key-value pair for the key request in the **kwargs of load_strategy which was passed on to the request parameter in get_stragegy , but that doesn't seem to be the case. 我期待会有一个键值对的关键request**kwargsload_strategy这是上传递的request参数get_stragegy ,但似乎并不如此。

I am trying to figure out what I am missing here. 我想弄清楚我在这里缺少什么。

You are passing in an extra positional argument: 你传递了一个额外的位置参数:

args = ([],)

There is one value in that tuple, a list object. 该元组中有一个值,即列表对象。 It is applied after the other three arguments passed to get_strategy() , so to request . 在将其他三个参数传递给get_strategy()之后应用它,以便request Python sees you calling: Python看到你调用:

get_strategy("backends", "strategy", "storage", [],
             request="myReq", backend="myBackend", redirect_uri=None, 
             acess_token="myAccToken", id="myId")

and the 4 positional arguments are applied against the backends , strategy , storage and request parameters respectively. 并且分别对backendsstrategystoragerequest参数应用4个位置参数。

If you meant to pass in 3 positional arguments, then specify args as an empty tuple: 如果你打算传入3个位置参数,那么将args指定为空元组:

args = ()

and things work just fine: 事情很好:

>>> def get_strategy(backends, strategy, storage, request=None, backend=None, *args, **kwargs):
...     print request
... 
>>> def load_strategy(*args, **kwargs):
...     get_strategy("backends", "strategy", "storage", *args, **kwargs)
... 
>>> args = ()
>>> kwargs = {"acess_token":"myAccToken", "id":"myId"}
>>> load_strategy(request="myReq", backend="myBackend", redirect_uri=None, *args, **kwargs)
myReq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用** kwargs时关键字参数的多个值 - multiple values for keyword argument when using **kwargs 使用参数,关键字参数,* args,** kwargs与Python函数混淆 - Confusion with Python functions using an argument, keyword argument, *args, **kwargs 如何为* args和** kwargs修复“为参数提供多个值”错误? - How to fix 'got multiple values for argument' error for *args and **kwargs? Python error: function got multiple values for keyword argument for value - Python error : function got multiple values for keyword argument for value Python关键字args vs kwargs - Python keyword args vs kwargs 在构造函数中使用** kwargs时出现意外的关键字参数 - Unexpected keyword argument when using **kwargs in constructor 带有其他指定关键字参数的python函数* args和** kwargs - python function *args and **kwargs with other specified keyword arguments spider = cls(* args,** kwargs)TypeError:__init __()得到了意外的关键字参数&#39;_job&#39; - spider = cls(*args, **kwargs) TypeError: __init__() got an unexpected keyword argument '_job' Odoo,Python:ValueError:write()为关键字参数“ context”获得了多个值 - Odoo, Python : ValueError: write() got multiple values for keyword argument 'context' 使用字典编写数据库时“对象的关键字参数有多个值” - “object got multiple values for keyword argument” when writing database using dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM