<br><br><div class="gmail_quote">On Sat, Mar 15, 2008 at 3:21 PM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</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;">
This post does point out an inconistency in Thomas's patch:<br>
<br>
def f():<br>
yield 1, 2, 3<br>
<br>
Yields a single three-tuple.<br>
<br>
But<br>
<br>
def f():<br>
yield *[1, 2], 3<br>
<br>
Yields three separate values.</blockquote><div><br>Uhm, what? It doesn't, and it shouldn't, and I'm not sure what makes you think it does.<br><br>>>> def f():<br>... yield *[1, 2], 3<br>... <br>>>> list(f())<br>
[(1, 2, 3)]<br><br>The comma makes the argument to 'yield' a single item (a tuple) and the '*' is part of the item in the tuple. In other words, the * binds more tightly than the comma (which is not something I changed; the parser already worked this way and it's necessary for '*a, b, c = ..' to even work. 'yield *[1, 2], 3' is parsed as 'yield (*[1, 2], 3)', not 'yield *([1, 2], 3)'. What you suggest (three separate values) requires 'yield *(*[1, 2], 3)'.<br>
<br>>>> def f1():<br>... yield (*[1, 2], 3)<br>... <br>>>> list(f1())<br>[(1, 2, 3)]<br><br>>>> def f2():<br>... yield *([1, 2], 3)<br>... <br>>>> list(f2())<br>[[1, 2], 3]<br><br>
>>> def f3():<br>... yield *(*[1, 2], 3)<br>... <br>>>> list(f3())<br>[1, 2, 3]<br><br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I think this is inconsistent, since<br>
<br>
(1, 2, 3)<br>
<br>
and<br>
<br>
(*[1, 2], 3)<br>
<br>
are equivalent, and so are<br>
<br>
yield 1, 2, 3<br>
<br>
and<br>
<br>
yield (1, 2, 3)</blockquote><div><br>Yes, they are indeed all equivalent :-)<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
I'm not sure how to solve this except by adopting a different syntax<br>
for the multiple-yield. Perhaps<br>
<br>
*yield x<br>
<br>
instead of<br>
<br>
yield *x<br>
<br>
???</blockquote><div><br>If there really were an inconsistency here, I would certainly not suggest fixing it that way, yuuueghh.<br><br></div><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"><br>
On Sat, Mar 15, 2008 at 4:58 PM, Terry Reedy <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>> wrote:<br>
><br>
</div><div><div></div><div class="Wj3C7c">> "Guido van Rossum" <<a href="mailto:guido@python.org">guido@python.org</a>> wrote in message<br>
> news:ca471dc20803150915l27750346g8655f596f5035c69@mail.gmail.com...<br>
><br>
> | Thomas Wouters suggests some new syntax:<br>
><br>
> I see this as two suggestions:<br>
> 1. Complete the extension of the validity of *expression syntax from<br>
> function call/definition to expression/assignment.<br>
> 2. Give *expression a related meaning in yield statements.<br>
><br>
><br>
> |<br>
> | <a href="http://bugs.python.org/issue2292" target="_blank">http://bugs.python.org/issue2292</a><br>
> |<br>
> | >>> a, b, *c = range(5)<br>
> |<br>
> | >>> *a, b, c = a, b, *c<br>
> | >>> a, b, c<br>
> | ([0, 1, 2], 3, 4)<br>
> | >>> [ *a, b, c ]<br>
> | [0, 1, 2, 3, 4]<br>
><br>
> I understand 'f(*a)' to mean 'execute the expression as if the items of<br>
> iterable a has been written literally in the code (with 'as if'<br>
> optimizations allowed for special cases). The above follow the same rule.<br>
> +1<br>
> A tutorial written for Py3 without regard to history should then introduce<br>
> *expressions in examples like the above first, and only later the usage for<br>
> functions, as if things had been this way all along.<br>
><br>
><br>
> | >>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]<br>
> | >>> [ *item for item in L ]<br>
> | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br>
><br>
> Since above will become equivalent to list(gen_exp), I see this as being<br>
> tied to yield *exp.<br>
><br>
><br>
> | Also, yielding everything from an iterator:<br>
> |<br>
> | >>> def flatten(iterables):<br>
> | ... for it in iterables:<br>
> | ... yield *it<br>
><br>
> Following the general rule above for *exp, that would be the same as yield<br>
> tuple(it). 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>
><br>
> 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.<br>
> Raise an exception? That establishes/requires a new grammatical<br>
> category -- yield_expression. But I would see this as preferable to the<br>
> clash.<br>
><br>
> Terry Jan Reedy<br>
><br>
><br>
><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" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
</div></div>> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/guido%40python.org" target="_blank">http://mail.python.org/mailman/options/python-3000/guido%40python.org</a><br>
<div class="Ih2E3d">><br>
<br>
<br>
<br>
--<br>
--Guido van Rossum (home page: <a href="http://www.python.org/%7Eguido/" target="_blank">http://www.python.org/~guido/</a>)<br>
_______________________________________________<br>
</div><div><div></div><div class="Wj3C7c">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" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/thomas%40python.org" target="_blank">http://mail.python.org/mailman/options/python-3000/thomas%40python.org</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <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!