[Python-3000] PEP 3132: Extended Iterable Unpacking (original) (raw)

Georg Brandl 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.

Georg

PEP: 3132 Title: Extended Iterable Unpacking Version: RevisionRevisionRevision Last-Modified: DateDateDate Author: Georg Brandl <georg at python.org> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 3.0 Post-History:

Abstract

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)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]

Rationale

Many algorithms require splitting a sequence in a "first, rest" pair. With the new syntax, ::

first, rest = seq[0], 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.

Specification

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 items.

It is an error (as it is currently) if the iterable doesn't contain enough items to assign to all the mandatory expressions.

Implementation

The proposed implementation strategy is:

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 benevolently.

Open Issues

References

None yet.

Copyright

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 mailing list