[Python-ideas] How assignment should work with generators?
C Anthony Risinger
c at anthonyrisinger.com
Tue Nov 28 02:06:05 EST 2017
On Nov 28, 2017 12:32 AM, "Steven D'Aprano" <steve at pearwood.info> wrote:
On Tue, Nov 28, 2017 at 06:15:47PM +1300, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >How does "stop iterating here" equate to a wildcard?
>
> The * means "I don't care what else the sequence has in it".
>
> Because I don't care, there's no need to iterate any further.
I'll grant you that. But I don't see how that relates to being a
wildcard. I'm not seeing the connection. I mean, you wouldn't interpret
from module import *
to mean "I don't care what's in the module, so don't bother importing
anything" would you? So the concept of wildcard here seems to be the
opposite to its use here:
- in imports, it means "import everything the module offers";
- in extended iterable unpacking, it means "collect everything";
both of which (to me) seem related to the concept of a wildcard; but in
this proposed syntax, we have
x, y, * = iterable
which means the opposite to "collect everything", instead meaning
"collect nothing and stop".
Anyway, given that * = is visually ambiguous with *= I don't think this
syntax is feasible (even though * = is currently a syntax error, or at
least it is in 3.5).
If not already considered, what if the RHS had to be explicitly unpacked?
Something like:
a, b, c = *iterator
Which would essentially be:
a, b, c = (*iterator,)
This enables lazy assignment by default but `*` can force complete
expansion (and exact matching) of the RHS.
It's a breaking change, but it does have a straightforward fix (simply wrap
and unpack any relevant RHS).
Thanks,
--
C Anthony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171128/fd0ec6c1/attachment-0001.html>
More information about the Python-ideas
mailing list