** reference to a Variable
本问题已经有最佳答案,请猛点这里访问。
我有那个变量
1 2 3 4 | data = { 'username' : self.username, 'attributes' : self._get_attrs(), } |
当我像**数据一样引用它时,它意味着什么?
型
来自
If the syntax"**expression" appears in the function call,
"expression" must evaluate to a mapping, the contents of which are
treated as additional keyword arguments. In the case of a keyword
appearing in both"expression" and as an explicit keyword argument, a
"TypeError" exception is raised.
号
请参见了解Python中的Kwargs。
还有pep:448——其他解包概括:
1 2 | >>> {**{'a': 1, 'b': 2}, **{'a': 3, 'c': 4}} {'b': 2, 'a': 3, 'c': 4} |
号
型
1 2 3 4 5 6 7 8 9 10 | def func(username=None, attributes=None): print(username) data = { 'username' :"Bob", 'attributes' : {}, } func(**data) # results in"Bob" |
它还可以用于收集关键字参数(Kwargs),如本问题中有关*args和**Kwargs的内容所示。