[Python-3000] Using *a for packing in lists and other places

Thomas Wouters thomas at python.org
Sun Mar 16 15:54:28 CET 2008


On Sat, Mar 15, 2008 at 9:51 PM, Neil Toronto <ntoronto at cs.byu.edu> wrote:

> Thomas Wouters wrote:
> >
> > On Sat, Mar 15, 2008 at 2:58 PM, Terry Reedy <tjreedy at udel.edu
> > <mailto:tjreedy at udel.edu>> wrote:
> >
> >     | Also, yielding everything from an iterator:
> >     |
> >     | >>> def flatten(iterables):
> >     | ...     for it in iterables:
> >     | ...         yield *it
> >
> >     Following the general rule above for *exp, that would be the same as
> >     yield
> >     tuple(it).
> >
> > No. *exp by itself is not valid syntax:
>
> Why isn't it? I'd rather have one meaning for the *-prefix operator with
> a single special case for assignment targets, than have a bunch of
> special cases scattered across the grammar.
>

Ehm, the only specialcases in the grammar are for *args and **kwargs in
functiondefinitions and functioncalls. There is no specialcase for *exp in
either store-context or load-context -- as a matter of fact, the patch to
add unpacking in load-context doesn't change the grammar at all.

That *exp by itself is not valid syntax is not new in this patch (PEP-3132
specifies it that way,) and it's easily explained: what the heck would it
mean? How does

t = a, b, c

differ from

*t = a, b, c

? In the exact same way, a lone '*exp' in load-context doesn't mean
anything. The 'unpacking' unpacks into the surrounding context, and without
context it's unclear what the result will be. To clarify,

{*a, b, c, *d} -> a set (dupes removed, order lost)
{*a} -> same
[*a, b, c, *d] -> a list (dupes intact, order intact)
[*a] -> same
(*a, b, c, *d) -> a tuple (dupes intact, order intact)
*a, b, c, *d -> same
(*a,) -> same
*a, -> same (but horrible style)
*a -> ?

The final reduction of (*a, b) is not *a, it's (*a,), just like is the case
for (a, b) and (a,). You can say '*exp by itself always unpacks into a
list', which Guido argued last night over beer, but that leave that

*a = z
a = *z

do the exact same thing (and the exact same thing as 'a = list(z)' but
something quite subtly but importantly different from 'a = z'), and

a, b, c = *z

differs in no way from

a, b, c = z

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20080316/da1c598e/attachment.htm 


More information about the Python-3000 mailing list