Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When programming Python I sometimes do a ** to make a conversion. I understand what it does but what data structures am I manipulating? A dict and what is the other? An array? Is there a name for the ** operator?

share|improve this question
exponential operator? – Rook Jan 23 '12 at 17:48
There are two meanings for **. Power and "keyword argument dictionary". Which are you talking about? The documentation has these words: " If the form “**identifier” is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary." Which of them seem relevant to your question? – S.Lott Jan 23 '12 at 18:03
"keyword argument dictionary" was the one I'm asking about. Thank you for the comments. – Programmer 400 Jan 24 '12 at 5:38
up vote 39 down vote accepted

It's not an operator as such, so it doesn't really have a name, but it is defined as a "syntactic rule". So it should be called:

  • "the keyword argument unpacking syntax"

If you have a list of arguments, *args, it's called "argument unpacking", in the same manner **kwargs is called "keyword argument unpacking".

If you use it on the left hand side of an =, as in a, *middle, end = my_tuple, you'd say "tuple unpacking".

In total, there are three types of (single parameter) arguments:

def f(x)  # x: positional argument
def f(x, y=0)  # y: keyword argument
def f(x, *xs, y=0)  # y: keyword-only argument

The *args argument is called the "variable positional parameter" and **kwargs is the "variable keyword parameter". Keyword-only arguments can't be given positionally, because a variable positional parameter will take all of the arguments you pass.

Most of this can be found in PEPs 0362 and 3102, as well as in the Control Flow section of the docs. It should be noted though that the function signature object PEP is only a draft, and the terminology might just be one person's idea. But they are good terms anyway. :)

So the * and ** arguments just unpack their respective data structures:

args = (1, 2, 3)  # usually a tuple, always an iterable*

f(*args) → f(1, 2, 3)

# and 

kwargs = {"a": 1, "b": 2, "c": 3}  # usually a dict, always a mapping*

f(**kwargs) -> f(1, 2, 3) # if ỳou have `def f(a=0, b=0, c=0)`

*: Iterables are objects that implement the __iter__() method and mappings are objects that implement __iter__() and __getitem__(). Any object that supports this protocol will be understood by the constructors tuple() and dict(), so they can be used for unpacking arguments.

share|improve this answer
Thanks for a very nice answer! – Programmer 400 Jan 24 '12 at 5:38
In case anyone else is confused, def f(x, *xs, y=0): pass is not valid Python 2.{5,6,7} syntax, nor does def f(x, y=0, *xs): do what you might expect. AFAIK, the only way to achieve the (obviously) intended effect is def f(x, *xs, **kw): y=kw.get('y', 0); del kw; .... Python 3 handles the original syntax as expected. – chbrown Sep 14 '13 at 18:08
While we're at it: the begin, *middle, end = (0, 1, 2, 3, 4, 5) syntax doesn't work in Python 2.x either. – Stefano Palazzo Sep 14 '13 at 22:29

I don't think it has a name. In the Python Docs under "Unpacking Argument Lists", it's just referred to as "the **-operator."

I'm not sure what you mean by "the other" data structure. When you do f(**kwargs) you unpack the dictionary kwargs as a sequence of key-value pairs. I don't see that there's another structure involved.

I'll copy the example in the above documentation for clarity.

>>> def parrot(voltage, state='a stiff', action='voom'):
...     print "-- This parrot wouldn't", action,
...     print "if you put", voltage, "volts through it.",
...     print "E's", state, "!"
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

See also: What does *args and **kwargs mean?

share|improve this answer
There are several possible ways a reader may interpret f(**kwargs)... – Deer Hunter Jul 7 '13 at 17:25

If you are unsure what to call a particular operator or if it is unnamed, you can always resort to Waka Waka Bang Splat as a reference to help you figure out what to call it. In this case for ** I would call it double-splat, though there are some alternate names for symbols.

share|improve this answer

protected by gnat Sep 9 '15 at 14:38

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.