[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension
Random832
random832 at fastmail.com
Thu Oct 13 15:46:11 EDT 2016
On Thu, Oct 13, 2016, at 14:50, David Mertz wrote:
> Neither of those follows conventional Python semantics for function
> calling
> or sequence unpacking. So maybe that remains a type error or syntax
> error. But then we exclude a very common pattern of using comprehensions
> to create collections of *transformed* data, not simply of filtered data.
[*map(math.exp, t) for t in [(1, 2), (3, 4)]]
[*(math.exp(x) for x in t) for t in [(1, 2), (3, 4)]]
I think "excluding" is a bit of a strong word - just because something
doesn't address a mostly unrelated need doesn't mean it doesn't have any
merit in its own right. Not every proposal is going to do everything. I
think the key is that the person originally asking this thought of *x as
a generalized "yield from x"-ish thing, for example:
"a, *b, c" becomes "def f(): yield a; yield from b; yield c;"
[a, *b, c] == list(f())
(a, *b, c) == tuple(f())
so, under a similar 'transformation', "*foo for foo in bar" likewise
becomes "def f(): for foo in bar: yield from foo"
bar = [(1, 2), (3, 4)]
(*(1, 2), *(3, 4)) == == tuple(f())
[*(1, 2), *(3, 4)] == == list(f())
> In contrast, either of these are unambiguous and obvious:
>
> [math.exp(t) for t in flatten([(1,2),(3,4)])]
>
> Or:
>
> [math.exp(n) for t in [(1,2),(3,4)] for n in t]
>
> Obviously, picking math.exp() is arbitrary and any unary function would
> be
> the same issue.
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons. Intellectual property is
> to the 21st century what the slave trade was to the 16th.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list