[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension
Random832
random832 at fastmail.com
Thu Oct 13 23:32:49 EDT 2016
On Thu, Oct 13, 2016, at 18:15, Steven D'Aprano wrote:
> Consider the analogy with f(*t), where t = (a, b, c). We *don't* have:
>
> f(*t) is equivalent to f(a); f(b); f(c)
I don't know where this "analogy" is coming from.
f(*t) == f(a, b, c)
[*t] == [a, b, c]
{*t} == {a, b, c}
All of this is true *today*.
t, u, v = (a, b, c), (d, e, f), (g, h, i)
f(*t, *u, *v) == f(a, b, c, d, e, f, g, h, i)
[*t, *u, *v] == [a, b, c, d, e, f, g, h, i]
> > is very confusing, from what I understand: what does the `*` even mean
> > here.
>
> Indeed. The reader may be forgiven for thinking that this is yet another
> unrelated and arbitrary use of * to join the many other uses:
How is it arbitrary?
> - mathematical operator;
> - glob and regex wild-card;
> - unpacking;
This is unpacking. It unpacks the results into the destination.
There's a straight line from [*t, *u, *v] to [*x for x in (t, u, v)].
What's surprising is that it doesn't work now.
I think last month we even had someone who didn't know about 'yield
from' propose 'yield *x' for exactly this feature. It is intuitive - it
is a straight-line extension of the unpacking syntax.
> - import all
> - and now yield from
More information about the Python-ideas
mailing list