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?
3 Answers
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[1]
f(*args) → f(1, 2, 3)
# and
kwargs = {"a": 1, "b": 2, "c": 3} # usually a dict, always a mapping*
f(**kwargs) -> f(a=1, b=2, c=3)
[1]: Iterables are objects that implement the __iter__()
method and mappings are objects that implement keys()
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.
-
3In case anyone else is confused,
def f(x, *xs, y=0): pass
is not valid Python 2.{5,6,7} syntax, nor doesdef f(x, y=0, *xs):
do what you might expect. AFAIK, the only way to achieve the (obviously) intended effect isdef f(x, *xs, **kw): y=kw.get('y', 0); del kw; ...
. Python 3 handles the original syntax as expected.– chbrownSep 14, 2013 at 18:08 -
1While we're at it: the
begin, *middle, end = (0, 1, 2, 3, 4, 5)
syntax doesn't work in Python 2.x either. Sep 14, 2013 at 22:29 -
This answer is incorrect in Python 3.5 and later. PEP-448 specifies the ** operator as the dictionary unpacking operator. See python.org/dev/peps/pep-0448– devnul3Apr 12, 2019 at 21:23
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?
-
There are several possible ways a reader may interpret
f(**kwargs)
... Jul 7, 2013 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.
**
. 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?