I've that variable
data = {
'username' : self.username,
'attributes' : self._get_attrs(),
}
What does it mean when I reference it like **data ?
I've that variable
data = {
'username' : self.username,
'attributes' : self._get_attrs(),
}
What does it mean when I reference it like **data ?
**
in **data
is a dictionary unpacking operator in Python. See What is the name of **
in python?
From help('CALLS')
:
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.
See Understanding kwargs in Python.
There is also PEP: 448 -- Additional Unpacking Generalizations:
>>> {**{'a': 1, 'b': 2}, **{'a': 3, 'c': 4}}
{'b': 2, 'a': 3, 'c': 4}
The **
expands your dictionary. Example:
def func(username=None, attributes=None):
print(username)
data = {
'username' : "Bob",
'attributes' : {},
}
func(**data)
# results in "Bob"
It can also be used to collect keyword arguments (kwargs), as seen in this question about *args and **kwargs.