Python 参数作为字典 [英] Python arguments as a dictionary
本文介绍了Python 参数作为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取作为字典传递给方法的参数名称及其值?
我想将 GET 请求的可选参数和必需参数指定为 HTTP API 的一部分,以便构建 URL.我不确定使这个 pythonic 的最佳方法.
解决方案
使用带有 **
前缀的单个参数.
How can I get argument names and their values passed to a method as a dictionary?
I want to specify the optional and required parameters for a GET request as part of a HTTP API in order to build the URL. I'm not sure of the best way to make this pythonic.
解决方案
Use a single argument prefixed with **
.
>>> def foo(**args):
... print(args)
...
>>> foo(a=1, b=2)
{'a': 1, 'b': 2}
这篇关于Python 参数作为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文