传递 kwargs 列表?

发布于 2024-08-06 00:19:50 字数 180 浏览 7 评论 0原文

为了简洁起见,我可以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佞臣 2024-08-13 00:19:50

是的。你可以这样做:

def method(**kwargs):
  print kwargs

keywords = {'keyword1': 'foo', 'keyword2': 'bar'}
method(keyword1='foo', keyword2='bar')
method(**keywords)

在 Python 中运行它确认这些产生相同的结果:

{'keyword2': 'bar', 'keyword1': 'foo'}
{'keyword2': 'bar', 'keyword1': 'foo'}

Yes. You do it like this:

def method(**kwargs):
  print kwargs

keywords = {'keyword1': 'foo', 'keyword2': 'bar'}
method(keyword1='foo', keyword2='bar')
method(**keywords)

Running this in Python confirms these produce identical results:

{'keyword2': 'bar', 'keyword1': 'foo'}
{'keyword2': 'bar', 'keyword1': 'foo'}
无声静候 2024-08-13 00:19:50

正如其他人指出的那样,您可以通过传递命令来做您想做的事。构建字典的方法有多种。保留您尝试的 keyword=value 样式的一种方法是使用 dict 内置:

keywords = dict(keyword1 = 'foo', keyword2 = 'bar')

注意 dict 的多功能性;所有这些都会产生相同的结果:

>>> kw1 = dict(keyword1 = 'foo', keyword2 = 'bar')
>>> kw2 = dict({'keyword1':'foo', 'keyword2':'bar'})
>>> kw3 = dict([['keyword1', 'foo'], ['keyword2', 'bar']])
>>> kw4 = dict(zip(('keyword1', 'keyword2'), ('foo', 'bar')))
>>> assert kw1 == kw2 == kw3 == kw4
>>> 

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:

keywords = dict(keyword1 = 'foo', keyword2 = 'bar')

Note the versatility of dict; all of these produce the same result:

>>> kw1 = dict(keyword1 = 'foo', keyword2 = 'bar')
>>> kw2 = dict({'keyword1':'foo', 'keyword2':'bar'})
>>> kw3 = dict([['keyword1', 'foo'], ['keyword2', 'bar']])
>>> kw4 = dict(zip(('keyword1', 'keyword2'), ('foo', 'bar')))
>>> assert kw1 == kw2 == kw3 == kw4
>>> 
月野兔 2024-08-13 00:19:50

你的意思是字典吗?当然你可以:

def method(**kwargs):
    #do something

keywords = {keyword1: 'foo', keyword2: 'bar'}
method(**keywords)

Do you mean a dict? Sure you can:

def method(**kwargs):
    #do something

keywords = {keyword1: 'foo', keyword2: 'bar'}
method(**keywords)
2024-08-13 00:19:50

因此,当我来到这里时,我正在寻找一种在一个函数中传递多个 **kwargs 的方法 - 以便以后在其他函数中使用。因为这并不令人惊讶,因为它不起作用:

def func1(**f2_x, **f3_x):
     ...

通过一些自己的“实验”编码,我找到了明显的方法:

def func3(f3_a, f3_b):
    print "--func3--"
    print f3_a
    print f3_b
def func2(f2_a, f2_b):
    print "--func2--"
    print f2_a
    print f2_b

def func1(f1_a, f1_b, f2_x={},f3_x={}):
    print "--func1--"
    print f1_a
    print f1_b
    func2(**f2_x)
    func3(**f3_x)

func1('aaaa', 'bbbb', {'f2_a':1, 'f2_b':2}, {'f3_a':37, 'f3_b':69})

这按预期打印:

--func1--
aaaa
bbbb
--func2--
1
2
--func3--
37
69

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:

def func1(**f2_x, **f3_x):
     ...

With some own 'experimental' coding I came to the obviously way how to do it:

def func3(f3_a, f3_b):
    print "--func3--"
    print f3_a
    print f3_b
def func2(f2_a, f2_b):
    print "--func2--"
    print f2_a
    print f2_b

def func1(f1_a, f1_b, f2_x={},f3_x={}):
    print "--func1--"
    print f1_a
    print f1_b
    func2(**f2_x)
    func3(**f3_x)

func1('aaaa', 'bbbb', {'f2_a':1, 'f2_b':2}, {'f3_a':37, 'f3_b':69})

This prints as expected:

--func1--
aaaa
bbbb
--func2--
1
2
--func3--
37
69
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文