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

Steven D'Aprano steve at pearwood.info
Sat Oct 15 04:09:58 EDT 2016


On Fri, Oct 14, 2016 at 08:29:28PM +1300, Greg Ewing wrote:
> Steven D'Aprano wrote:
> 
>  So why would yield *t give us this?
> >
> >    yield a; yield b; yield c
> >
> >By analogy with the function call syntax, it should mean:
> >
> >    yield (a, b, c)
> 
> This is a false analogy, because yield is not a function.

Neither are list comprehensions or sequence unpacking in the context of 
assignment:

    a, b, c = *t

Not everything is a function. What's your point?

As far as I can see, in *every* other use of sequence unpacking, *t is 
conceptually replaced by a comma-separated sequence of items from t. If 
the starred item is on the left-hand side of the = sign, we might call 
it "sequence packing" rather than unpacking, and it operates to collect 
unused items, just like *args does in function parameter lists.

Neither of these are even close to what the proposed [*t for t in 
iterable] will do.


> >>However, consider the following spelling:
> >>
> >>   l = [from f(t) for t in iterable]
> 
> That sentence no verb!
> 
> In English, 'from' is a preposition, so one expects there
> to be a verb associated with it somewhere. We currently
> have 'from ... import' and 'yield from'.
> 
> But 'from f(t) for t in iterable' ... do what?

*shrug* 

I'm not married to this suggestion. It could be written 
[MAGIC!!! HAPPENS!!! HERE!!! t for t in iterable] if you prefer.
The suggestion to use "from" came from Sjoerd Job Postmus, not me.


-- 
Steve


More information about the Python-ideas mailing list