On Fri, Jun 18, 2021 at 09:33:49PM -0700, Guido van Rossum wrote:
> Now, this shouldn't be considered an airtight argument against [*chunk for
> ...], but it does show that there's no straightforward explanation of its
> meaning through equivalence (like the OP seemed to think), and I think this
> is what Serhiy was also getting at in his post.
Indeed. I was initially confused by what Ben thought was a simple and
obvious connection between star unpacking in some other contexts and his
suggestion for comprehensions. The analogy with `[*a]` never crossed my
mind, and I don't think that we should look at this as literally the
application of sequence unpacking in a comprehension, for reasons I gave
in my earlier post.
But having it explained to me, I think that treating this as an analogy
rather than literal unpacking works. We already give unary star and
double star a number of meanings, not all of which are related:
- import wildcard;
- capture positional and keyword parameters `def func(*args, **kw)`
- sequence and keyword unpacking in function calls;
- sequence capture in assignment targets `head, *a, tail = items`
- sequence unpacking in list etc displays;
Have I missed any?
We could define *star comprehensions* as syntactic sugar for nested
comprehensions, to aid in flattening nested sequences and mappings.
[*expression for name in sequence if condition]
results in this:
result = []
for name in sequence:
if condition:
for tmp in expression:
result.append(tmp)
return result
I haven't thought deeply into this, but I think that if the starred
expression is anything but a simple name, it may require parentheses?
*name # okay
*(name.attr or []) # everything else needs parens
Alternatively, we could just do something that people have been asking
about since Python 1.5 and provide a flatten builtin or list method :-)
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/7XEUJUQCEV57KVUSEY4C6ZDNZ3AHD2IS/
Code of Conduct: http://python.org/psf/codeofconduct/