[Python-3000] PEP 3132: Extended Iterable Unpacking

Guido van Rossum guido at python.org
Wed May 2 20:08:36 CEST 2007

> >>     >>> a, *b, c = range(5)
> >>     >>> a
> >>     0
> >>     >>> c
> >>     4
> >>     >>> b
> >>     [1, 2, 3]

> > Has it been pointed out to you already that this particular example is
> > hard to implement if the RHS is an iterator whose length is not known
> > a priori? The implementation would have to be quite hairy -- it would
> > have to assign everything to the list b until the iterator is
> > exhausted, and then pop a value from the end of the list and assign it
> > to c.

> Yes, that is correct. My implementation isn't *that* hairy, though, it's
> only 13 lines of code more.

OK. The PEP was kind of light on substance here. Glad you've thought about it.

> I'll post the patch to SourceForge later today.


> > it would be much easier if *b was only allowed at the end. (It
> > would be even worse if b were assigned a tuple instead of a list, as
> > per your open issues.)
> The created tuple is a fresh one, so can't I just copy pointers like from a
> list and set ob_size later?


> > Also, what should this do? Perhaps the grammar could disallow it?
> >
> > *a = range(5)
> I'm not so sure about the grammar, I'm currently catching it in the AST
> generation stage.

Hopefully it's possible to only allow this if there's at least one comma?

In any case the grammar will probably end up accepting *a in lots of
places where it isn't really allowed and you'll have to fix all of
those. That sounds messy; only allowing *a at the end seems a bit more
manageable. But I'll hold off until I can shoot holes in your
implementation. ;-)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list