传递 kwargs 列表?
为了简洁起见,我可以将 kwargs 列表传递给方法吗?这就是我正在尝试做的事情:
def method(**kwargs):
#do something
keywords = (keyword1 = 'foo', keyword2 = 'bar')
method(keywords)
Can I pass a list of kwargs to a method for brevity? This is what i'm attempting to do:
def method(**kwargs):
#do something
keywords = (keyword1 = 'foo', keyword2 = 'bar')
method(keywords)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。你可以这样做:
在 Python 中运行它确认这些产生相同的结果:
Yes. You do it like this:
Running this in Python confirms these produce identical results:
正如其他人指出的那样,您可以通过传递命令来做您想做的事。构建字典的方法有多种。保留您尝试的
keyword=value
样式的一种方法是使用 dict 内置:注意
dict
的多功能性;所有这些都会产生相同的结果:As others have pointed out, you can do what you want by passing a dict. There are various ways to construct a dict. One that preserves the
keyword=value
style you attempted is to use the dict built-in:Note the versatility of
dict
; all of these produce the same result:你的意思是字典吗?当然你可以:
Do you mean a dict? Sure you can:
因此,当我来到这里时,我正在寻找一种在一个函数中传递多个 **kwargs 的方法 - 以便以后在其他函数中使用。因为这并不令人惊讶,因为它不起作用:
通过一些自己的“实验”编码,我找到了明显的方法:
这按预期打印:
So when I've come here I was looking for a way to pass several **kwargs in one function - for later use in further functions. Because this, not that surprisingly, doesn't work:
With some own 'experimental' coding I came to the obviously way how to do it:
This prints as expected: