[Python-3000] PEP 3132: Extended Iterable Unpacking
g.brandl at gmx.net
Tue May 1 23:44:59 CEST 2007
This is a bit late, but it was in my queue by April 30, I swear! ;)
Comments are appreciated, especially some phrasing sounds very clumsy
to me, but I couldn't find a better one.
Title: Extended Iterable Unpacking
Author: Georg Brandl <georg at python.org>
Type: Standards Track
This PEP proposes a change to iterable unpacking syntax, allowing to
specify a "catch-all" name which will be assigned a list of all items
not assigned to a "regular" name.
An example says more than a thousand words::
>>> a, *b, c = range(5)
[1, 2, 3]
Many algorithms require splitting a sequence in a "first, rest" pair.
With the new syntax, ::
first, rest = seq, seq[1:]
is replaced by the cleaner and probably more efficient::
first, *rest = seq
For more complex unpacking patterns, the new syntax looks even
cleaner, and the clumsy index handling is not necessary anymore.
A tuple (or list) on the left side of a simple assignment (unpacking
is not defined for augmented assignment) may contain at most one
expression prepended with a single asterisk. For the rest of this
section, the other expressions in the list are called "mandatory".
Note that this also refers to tuples in implicit assignment context,
such as in a ``for`` statement.
This designates a subexpression that will be assigned a list of all
items from the iterable being unpacked that are not assigned to any
of the mandatory expressions, or an empty list if there are no such
It is an error (as it is currently) if the iterable doesn't contain
enough items to assign to all the mandatory expressions.
The proposed implementation strategy is:
- add a new grammar rule, ``star_test``, which consists of ``'*'
test`` and is used in test lists
- add a new ASDL type ``Starred`` to represent a starred expression
- catch all cases where starred expressions are not allowed in the AST
and symtable generation stage
- add a new opcode, ``UNPACK_EX``, which will only be used if a
list/tuple to be assigned to contains a starred expression
- change ``unpack_iterable()`` in ceval.c to handle the extended
Note that the starred expression element introduced here is universal
and could be used for other purposes in non-assignment context, such
as the ``yield *iterable`` proposal.
The author has written a draft implementation, but there are some open
issues which will be resolved in case this PEP is looked upon
- Should the catch-all expression be assigned a list or a tuple of items?
This document has been placed in the public domain.
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.
More information about the Python-3000