On Thu, Oct 13, 2016, at 16:42, Paul Moore wrote:
> I remain puzzled.
> Given the well-documented and understood transformation:
> [fn(x) for x in lst if cond]
> translates to
> result = []
> for x in lst:
>    if cond:
>       result.append(fn(x))
> please can you explain how to modify that translation rule to
> incorporate the suggested syntax?

In this case * would change this to result.extend (or +=)

just as result = [a, *b, c] is equivalent to:
result = []

result = [*x for x in lst if cond] would become:
result = []
for x in lst:
   if cond:

I used yield from as my original example to include generator
expressions, which should also support this.

> Personally, I'm not even sure any more that I can *describe* the
> suggested syntax. Where in [fn(x) for x in lst if cond] is the *
> allowed? fn(*x)?

This already has a meaning, so it's obviously "allowed", but not in a
way relevant to this proposal. The elements of x are passed to fn as
arguments rather than being inserted into the list. Ultimately the
meaning is the same.

> *fn(x)? Only as *x with a bare variable, but no expression?

Both of these would be allowed. Any expression would be allowed, but at
runtime its value must be iterable, the same as other places that you
can use *x.
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to