On Sat, Mar 15, 2008 at 2:58 PM, Terry Reedy <[EMAIL PROTECTED]> 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: >>> 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. So, >>> a, b = (*c,) (or [*c]) works, but is effectively the same thing as >>> a, b = c > But that is nearly useless, whereas the the implicit inner for > loop meaning is quite useful, with, perhaps, a speedup over an explicit > inner loop. Since yield is already pretty magical,a bit more might not > hurt. > > But, ... what do you do with > yield *a,b,c # a,b,c as above? > Yield a 5-tuple? That would clash badly with 'yield *a' not yielding a > 3-tuple. It yields a 5-tuple, yes. Does it help your confusion if you write it as: >>> yield (*a, b, c) ? The context of the unpacking operation isn't 'yield', it's the tuple you create with the commas. If you want it to yield all the elements in a, followed by b and c, you would need: >>> yield *(*a, b, c) It's quite like function arguments (except you can only specify *args after all positional arguments, right now; Guido wants that to change anyway.) -- Thomas Wouters <[EMAIL PROTECTED]> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
_______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
