传递名单清单?

问题:传递名单清单?

我可以将简短的清单传递给一种简洁的方法吗?这是我正在尝试做的事情:

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)

回答 0

是。您可以这样操作:

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'}

回答 1

正如其他人指出的那样,您可以通过传递命令来做您想做的事情。有多种方法可以构建字典。保留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
>>> 

回答 2

你是说一个命令吗?你当然可以:

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)

回答 3

因此,当我来到这里时,我正在寻找一种在一个函数中传递多个** kwarg的方法-供以后在其他函数中使用。因为这并不奇怪,所以不起作用:

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