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

Steven D'Aprano steve at pearwood.info
Wed Oct 26 12:27:07 EDT 2016


On Wed, Oct 26, 2016 at 01:25:48AM +0100, Rob Cliffe wrote:

> (2) This is admittedly a somewhat tangential argument, but: I didn't 
> really know what "yield from" meant.  But when I read in an earlier post 
> that someone had proposed "yield *" for it, I had a Eureka moment.  

Are you aware that "yield from it" does not just mean this...?

for x in it:
    yield x


"yield from ..." is not just "some sort of unpacking". If all it did was 
iterate over an iterable and yield the values, it would not have been 
given special syntax just to save one line. It does *much* more than 
just those two lines.

For start, it is an expression which returns a value, so you can write:

result = yield from it

A full implementation of "yield from ..." would be 39 lines of Python 
code, according to the PEP, not two. It has to handle delegating send(), 
throw() and close() messages, exceptions, plus of course the obvious 
iteration.


> Which suggests if  "*" is used to mean some sort of unpacking in more 
> contexts, the more familiar and intuitive it may become.  I guess the 
> word I'm groping for is 'consistency'.

I think that there is zero hope of consistency for * the star operator. 
That horse has bolted. It is already used for:

- multiplication and exponentiation
- repetition
- "zero or more of the previous element" in regular expressions
- "zero or more of any character" in globbing
- "everything" in imports
- sequence unpacking
- sequence packing
- collecting positional and keyword arguments

Some of which are admittedly *similar* uses, but the * operator does get 
overloaded for so many unrelated uses.


-- 
Steve


More information about the Python-ideas mailing list