0

I've that variable

    data = {
        'username' : self.username,
        'attributes' : self._get_attrs(),
        }

What does it mean when I reference it like **data ?

Stefano
  • 3,127
  • 2
  • 27
  • 33

2 Answers2

1

** 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}
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

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.

Community
  • 1
  • 1
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • 1
    Also, in Python 3.5+, [it can be used more generally to construct `dict`s from other mappings.](https://www.python.org/dev/peps/pep-0448/), e.g. combining two `dict`s to make a new third `dict` with `combined = {**dict1, **dict2}`. – ShadowRanger Feb 26 '16 at 12:00