[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension

אלעזר elazarg at gmail.com
Sat Oct 15 13:33:27 EDT 2016


On Sat, Oct 15, 2016 at 1:49 PM Steven D'Aprano <steve at pearwood.info> wrote:
...

> And the transformation of *t for the items of t (I don't care if it is a
> real transformation in the implementation, or only a fictional
> transformation) cannot work in a list comp. Let's make the number of
> items of t explicit so we don't have to worry about variable item
> counts:
>
>     [*t for t in iterable]  # t has three items
>     [a, b, c for (a, b, c) in iterable]
>
>
> That's a syntax error. To avoid the syntax error, we need parentheses:
>
>     [(a, b, c) for (a, b, c) in iterable]
>
> and that's a no-op.


You are confusing here two distinct roles of the parenthesis:
disambiguation as in "(1 + 2) * 2", and tuple construction as in (1, 2, 3).
This overload is the reason that (1) is not a 1-tuple and we must write
(1,).

You may argue that this overloading causes confusion and make this
construct hard to understand, but please be explicit about that; even if
<1, 2,3 > was the syntax for tuples, the expansion was still

 [(a, b, c) for (a, b, c) in iterable]

Since no tuple is constructed here.

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161015/a06915f6/attachment.html>


More information about the Python-ideas mailing list