0%

Extended Iterable Unpacking

The following syntax looks very handy in many case.

1
2
3
4
5
6
7
8
Python 3.7.3 (default, Mar 27 2019, 09:23:15)
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a, *args = [1, 2, 3, 4]
>>> a
1
>>> args
[2, 3, 4]

I can understand what does those code try to do easily. Yeah, this is the magic of Python. :-)
But I had a hard time to find the semantic explanation for the syntax.

I was thinking about

  1. Arbitrary Argument Lists
    But this is not function signature. And Arbitrary Argument Lists will construct *args as a tuple rather than a list.

  2. Unpacking Argument Lists
    As the name suggested, this syntax unpacking a list or tuple rather than create a new list.

Extended Iterable Unpacking

This is a new feature introduced in Python 3.0

I believe the posted code is already explained the usage of this syntax. Find more details from PEP 3132