Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are python's unpacking operators * and ** used?

The unpacking/splat operators * and ** differ widely in their applicability across python versions (2.7, 3.x < 3.5 and 3.x >= 3.5).

For example:

                                   |   2.7    |   3.1-3.4  |   3.5   
----------------------------------------------------------------------
function(*args)                         ✓            ✓          ✓    

x, *y, z = [1, 2, 3, 4, 5]              x            ✓          ✓    

{**x, **y}                              x            x          ✓    

Are there any more discrepancies between the various versions that I've missed? I'm looking through PEP and Readmes but the docs aren't detailed with this.

like image 384
cs95 Avatar asked Jul 27 '17 09:07

cs95


People also ask

What does * list mean in Python?

Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times. The following program repeats the list the given number of times using the * operator −

Why do we use asterisk (*) before name in function definition in Python?

It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list.

Does * unpack list Python?

How do you unpack a list in Python? Summary. Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.

What does * Before a variable mean in Python?

Practical Data Science using Python Single asterisk as used in function declaration allows variable number of arguments passed from calling environment. Inside the function it behaves as a tuple.


1 Answers

Around 1992 (not sure about Python version). This is from the Grammar file of Python 1.0.1.

# 06-Apr-92:
#   Use only '*' for varargs list

# 31-Mar-92:
#   Tighten syntax for try statements

# 27-Feb-92:
#   Allow NEWLINE* after eval input

# 16-Jan-92:
#   Added '*' as alternative for '+' in varargs syntax
#   (Not sure which alternative is better yet.)

# 11-Jan-92:
#   Variable length argument list syntax added: def f(a, b, +rest): ...

Python 1.4+:

Keyword Arguments: Functions and methods written in Python can now be called using keyword arguments of the form keyword = value.

Python 1.6+

There's now special syntax that you can use instead of the apply() function. f(*args, **kwds) is equivalent to apply(f, args, kwds). You can also use variations f(a1, a2, *args, **kwds) and you can leave one or the other out: f(*args), f(**kwds).

Python <= 2.7:

Tuple parameter unpacking was removed in Python 3.0.

PEP 3113: Tuple parameter unpacking removed. You can no longer write def foo(a, (b, c)): .... Use def foo(a, b_c): b, c = b_c instead.

Python 3.0+

PEP 3132: Extended Iterable Unpacking. You can now write things like a, b, *rest = some_sequence. And even *rest, a = stuff. The rest object is always a (possibly empty) list; the right-hand side may be any iterable.

PEP 3102: Keyword-only arguments. Named parameters occurring after *args in the parameter list must be specified using keyword syntax in the call. You can also use a bare * in the parameter list to indicate that you don’t accept a variable-length argument list, but you do have keyword-only arguments

Python 3.5+

PEP 448, additional unpacking generalizations.


As far as I know there's no single page that lists all the syntax changes. Per version syntax changes are listed in the What's new in Python section or you could check the Grammar specification of each release to see the differences.

like image 89
Ashwini Chaudhary Avatar answered Sep 24 '22 21:09

Ashwini Chaudhary