什么是这个python语法dict(name = name)?

时间:2017-05-09 16:34:32

标签: python dictionary syntax parameter-passing keyword-argument

我在the srapy documentation中遇到过这种语法。

>>> abc = ['a', 'b', 'c']
>>> dict(abc=abc)
{'abc': ['a', 'b', 'c']}

the python dict documentation中似乎没有提到这种语法。这个语法叫什么?

2 个答案:

答案 0 :(得分:3)

使用keyword arguments

大致相同:

def make_dict(**kwargs):
    return kwargs

在你的情况下,

abc = ['a', 'b', 'c']
dict(abc=abc)

表示:

dict(abc=['a', 'b', 'c'])

与:

相同
{'abc': ['a', 'b', 'c']}

答案 1 :(得分:1)

没有什么特别之处,dict()可以获取关键字参数以及位置参数。您可以阅读docs on dict()

因此,在您的代码段dict()中,只需使用单个关键字参数。