This reminds me a lot of haskell/prolog's head/tail list splitting. Looks like a good feature.<br><br>a*=range(5)<br>hmmn maybe in such a case, whenever there is the * operator, the resulting item is always a list/tuple, like the following:
<br> a=[[0,1,2,3,4]] ? <br><br>I have another question, what would happen in the case a*,b=tuple(range(5))<br><br>a = (0,1,2,3) ?<br><br>Should this keep the same type of container i.e. lists to lists and tuples to tuples or always convert to list?
<br><br>-Neville<br><br><div><span class="gmail_quote">On 5/2/07, <b class="gmail_sendername">Guido van Rossum</b> <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 5/1/07, Georg Brandl <<a href="mailto:g.brandl@gmx.net">g.brandl@gmx.net</a>> wrote:<br>> This is a bit late, but it was in my queue by April 30, I swear! ;)<br><br>Accepted.<br><br>> Comments are appreciated, especially some phrasing sounds very clumsy
<br>> to me, but I couldn't find a better one.<br>><br>> Georg<br>><br>><br>> PEP: 3132<br>> Title: Extended Iterable Unpacking<br>> Version: $Revision$<br>> Last-Modified: $Date$<br>> Author: Georg Brandl <
<a href="mailto:georg@python.org">georg@python.org</a>><br>> Status: Draft<br>> Type: Standards Track<br>> Content-Type: text/x-rst<br>> Created: 30-Apr-2007<br>> Python-Version: 3.0<br>> Post-History:
<br>><br>><br>> Abstract<br>> ========<br>><br>> This PEP proposes a change to iterable unpacking syntax, allowing to<br>> specify a "catch-all" name which will be assigned a list of all items
<br>> not assigned to a "regular" name.<br>><br>> An example says more than a thousand words::<br>><br>> >>> a, *b, c = range(5)<br>> >>> a<br>> 0<br>> >>> c
<br>> 4<br>> >>> b<br>> [1, 2, 3]<br><br>Has it been pointed out to you already that this particular example is<br>hard to implement if the RHS is an iterator whose length is not known<br>a priori? The implementation would have to be quite hairy -- it would
<br>have to assign everything to the list b until the iterator is<br>exhausted, and then pop a value from the end of the list and assign it<br>to c. it would be much easier if *b was only allowed at the end. (It<br>would be even worse if b were assigned a tuple instead of a list, as
<br>per your open issues.)<br><br>Also, what should this do? Perhaps the grammar could disallow it?<br><br>*a = range(5)<br><br>> Rationale<br>> =========<br>><br>> Many algorithms require splitting a sequence in a "first, rest" pair.
<br>> With the new syntax, ::<br>><br>> first, rest = seq[0], seq[1:]<br>><br>> is replaced by the cleaner and probably more efficient::<br>><br>> first, *rest = seq<br>><br>> For more complex unpacking patterns, the new syntax looks even
<br>> cleaner, and the clumsy index handling is not necessary anymore.<br>><br>><br>> Specification<br>> =============<br>><br>> A tuple (or list) on the left side of a simple assignment (unpacking<br>
> is not defined for augmented assignment) may contain at most one<br>> expression prepended with a single asterisk. For the rest of this<br>> section, the other expressions in the list are called "mandatory".
<br>><br>> Note that this also refers to tuples in implicit assignment context,<br>> such as in a ``for`` statement.<br>><br>> This designates a subexpression that will be assigned a list of all<br>> items from the iterable being unpacked that are not assigned to any
<br>> of the mandatory expressions, or an empty list if there are no such<br>> items.<br>><br>> It is an error (as it is currently) if the iterable doesn't contain<br>> enough items to assign to all the mandatory expressions.
<br>><br>><br>> Implementation<br>> ==============<br>><br>> The proposed implementation strategy is:<br>><br>> - add a new grammar rule, ``star_test``, which consists of ``'*'<br>> test`` and is used in test lists
<br>> - add a new ASDL type ``Starred`` to represent a starred expression<br>> - catch all cases where starred expressions are not allowed in the AST<br>> and symtable generation stage<br>> - add a new opcode, ``UNPACK_EX``, which will only be used if a
<br>> list/tuple to be assigned to contains a starred expression<br>> - change ``unpack_iterable()`` in ceval.c to handle the extended<br>> unpacking case<br>><br>> Note that the starred expression element introduced here is universal
<br>> and could be used for other purposes in non-assignment context, such<br>> as the ``yield *iterable`` proposal.<br>><br>> The author has written a draft implementation, but there are some open<br>> issues which will be resolved in case this PEP is looked upon
<br>> benevolently.<br>><br>><br>> Open Issues<br>> ===========<br>><br>> - Should the catch-all expression be assigned a list or a tuple of items?<br>><br>><br>> References<br>> ==========
<br>><br>> None yet.<br>><br>><br>> Copyright<br>> =========<br>><br>> This document has been placed in the public domain.<br>><br>><br>> --<br>> Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
<br>> Four shall be the number of spaces thou shalt indent, and the number of thy<br>> indenting shall be four. Eight shalt thou not indent, nor either indent thou<br>> two, excepting that thou then proceed to four. Tabs are right out.
<br>><br>> _______________________________________________<br>> Python-3000 mailing list<br>> <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>> <a href="http://mail.python.org/mailman/listinfo/python-3000">
http://mail.python.org/mailman/listinfo/python-3000</a><br>> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/guido%40python.org">http://mail.python.org/mailman/options/python-3000/guido%40python.org
</a><br>><br><br><br>--<br>--Guido van Rossum (home page: <a href="http://www.python.org/~guido/">http://www.python.org/~guido/</a>)<br>_______________________________________________<br>Python-3000 mailing list<br><a href="mailto:Python-3000@python.org">
Python-3000@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-3000">http://mail.python.org/mailman/listinfo/python-3000</a><br>Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/nevillegrech%40gmail.com">
http://mail.python.org/mailman/options/python-3000/nevillegrech%40gmail.com</a><br></blockquote></div><br>