<br><br><div class="gmail_quote">On Sat, Mar 15, 2008 at 2:58 PM, Terry Reedy <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><div class="Ih2E3d">
| Also, yielding everything from an iterator:<br>
|<br>
| >>> def flatten(iterables):<br>
| ... for it in iterables:<br>
| ... yield *it<br>
<br>
</div>Following the general rule above for *exp, that would be the same as yield<br>
tuple(it).</blockquote><div><br>No. *exp by itself is not valid syntax:<br><br>>>> a, b = *c<br> File "<stdin>", line 1<br>SyntaxError: can use starred expression only as assignment target<br><br>
It needs something to unpack *into*, in the immediate context. So,<br><br>>>> a, b = (*c,)<br><br>(or [*c]) works, but is effectively the same thing as<br><br>>>> a, b = c<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
But that is nearly useless, whereas the the implicit inner for<br>
loop meaning is quite useful, with, perhaps, a speedup over an explicit<br>
inner loop. Since yield is already pretty magical,a bit more might not<br>
hurt.<br>
</blockquote><div> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
But, ... what do you do with<br>
yield *a,b,c # a,b,c as above?<br>
Yield a 5-tuple? That would clash badly with 'yield *a' not yielding a<br>
3-tuple.</blockquote><div><br>It yields a 5-tuple, yes. Does it help your confusion if you write it as:<br><br> >>> yield (*a, b, c)<br><br>? 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:<br>
<br>>>> yield *(*a, b, c)<br><br>It's quite like function arguments (except you can only specify *args after all positional arguments, right now; Guido wants that to change anyway.)<br><br></div></div>-- <br>
Thomas Wouters <<a href="mailto:thomas@python.org">thomas@python.org</a>><br><br>Hi! I'm a .signature virus! copy me into your .signature file to help me spread!