[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension
Greg Ewing
greg.ewing at canterbury.ac.nz
Fri Oct 14 02:15:35 EDT 2016
Paul Moore wrote:
> please can you explain how to modify that translation rule to
> incorporate the suggested syntax?
It's quite simple: when there's a '*', replace 'append'
with 'extend':
[*fn(x) for x in lst if cond]
expands to
result = []
for x in lst:
if cond:
result.extend(fn(x))
The people thinking that you should just stick the '*x'
in as an argument to append() are misunderstanding the
nature of the expansion. You can't do that, because
the current expansion is based on the assumption that
the thing being substituted is an expression, and
'*x' is not a valid expression on its own. A new rule
is needed to handle that case.
And I'm the one who *invented* that expansion, so I
get to say what it means. :-)
--
Greg
More information about the Python-ideas
mailing list