Python 3: starred expression to unpack a list

The *args calling convention is documented in the Expressions reference: If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, …, xN, and expression evaluates to a sequence y1, …, yM, …

Read more

What do ** (double star/asterisk) and * (star/asterisk) mean in a function call?

A single star * unpacks a sequence or collection into positional arguments. Suppose we have def add(a, b): return a + b values = (1, 2) Using the * unpacking operator, we can write s = add(*values), which will be equivalent to writing s = add(1, 2). The double star ** does the same thing …

Read more

Unpacking, extended unpacking and nested extended unpacking

My apologies for the length of this post, but I decided to opt for completeness. Once you know a few basic rules, it’s not hard to generalize them. I’ll do my best to explain with a few examples. Since you’re talking about evaluating these “by hand,” I’ll suggest some simple substitution rules. Basically, you might …

Read more