[Python-ideas] How assignment should work with generators?

Steve Barnes gadgetsteve at live.co.uk
Thu Nov 30 17:59:05 EST 2017

On 30/11/2017 22:26, Greg Ewing wrote:
> Steven D'Aprano wrote:
>> On Wed, Nov 29, 2017 at 07:33:54PM +0000, Steve Barnes wrote:
>>> Just a thought but what about a syntax something along the lines of:
>>> a, b, *remainder = iterable
>>> Where remainder becomes the iterable with the first two values 
>>> consumed by assigning to a & b. 
>> Guido's time machine strikes again. That has worked since 3.3 if not 
>> older.
> This is not quite the same thing -- the rest of the items
> are extracted and put in a new list. I think Steve Barnes is
> suggesting that the iterator should be left alone and
> bound to the * target instead.
> That would be an incompatible change.
That is what I was thinking of but surely it would be more efficient way 
to do this for generators and large iterators. Since the practical 
difference between remainder being the (partly exhausted) iterator and 
it being a list, (itself an iterator), containing the results of 
expanding the remainder of the original iterator is slight.

The only limitation would be that the syntax a, b, *remainder, c, d = 
iterator would be illegal.
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

This email has been checked for viruses by AVG.

More information about the Python-ideas mailing list