[Python-3000] Using *a for packing in lists and other places
tjreedy at udel.edu
Sun Mar 16 23:20:56 CET 2008
"Thomas Wouters" <thomas at python.org> wrote in message
news:9e804ac0803151513j495be5d8h22bba219148cf491 at mail.gmail.com...
| On Sat, Mar 15, 2008 at 2:58 PM, Terry Reedy <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
| > tuple(it).
| No. *exp by itself is not valid syntax:
That may be a fact for your patch but not for all possible patches ;-)
| >>> a, b = *c
| File "<stdin>", line 1
| SyntaxError: can use starred expression only as assignment target
| It needs something to unpack *into*, in the immediate context.
My main desire is that *iterable have a simple (easily learnable)
consistent meaning. (Part of consistency is having it work the same both
in and out of function calls).
The rule I suggested is 'execute the statement the same *as if* the
iterable items had been written in the code as a comma sequence'. In the
above, the result would be a tuple. (For targets, the meaning is similar:
*x means, in effect, x, x, etc, with x sized as needed at runtime.)
The fact that the '*' would be redundant in some contexts and therefore
useless should not necessarily make it syntax error in that context.
if bool(e): pass # and
if bool(e) is True: pass
are both allowed even thought equivalent to 'if e: pass'.
More information about the Python-3000