Python-仅使用** kwargs的键值

时间:2017-08-24 18:57:26

标签: python-2.7 kwargs

我正在努力弄清楚如何使用** kwargs,但也只使用我正在使用的其他参数中的键值。例如,我有一个脚本,它使用API​​为POST请求提交数据,但有时,我可能需要根据用户提供的其他参数多次访问API的端点。这是我的代码:

def apiExample(name, location, user, **kwargs):
print name
print location
url = "https://example.com/user?"+str(user)
...
url2 = "https://example2.com/user2?"+str(user2)
...
url3 = "https://example2.com/user3?"+str(user3)
...

然后我会使用这样的函数,并提供额外的KW参数:

apiExample("Bob", "Texas", 45582 , user2=20, user3=5625)

我知道我在这里遗漏了一些东西:我想要完成的是,我希望能够使用为我的附加用户2和用户3函数参数(在此示例中)提供的值作为我的url2的值和我的函数中的url3语句。

根据我的理解** kwargs,或者更确切地说**语法将采用我提供的所有其他关键字参数并将它们添加到字典中,所以我真的不知道如何从这里继续。

也可能有更好的方法来做到这一点!

非常感谢任何指导。

0 个答案:

没有答案