[Python-ideas] Generator unpacking

Mike Müller mmueller at python-academy.de
Fri Feb 12 08:53:45 EST 2016


Am 12.02.16 um 13:09 schrieb Edward Minnix:
> Hello,
> 
> I am a new programmer and have been using Python for a few months.
> 
> I was experimenting the other day with unpacking (lists, tuples, etc.) And I
> realized something:
> 
> when you type:
> 
>>>> a, b, *rest = count()
> 
> The interpreter gets caught in an infinite loop which I could not kill without
> terminating my REPL.
> 
> Would there be a way to add generators to the unpackables, even if it was only
> in the front?

You can use `islice` to get the first two value from `count`:

from itertools import count, islice

a, b = islice(count(), None, 2)

Mike

> Thanks,
> Ed M
> 
> 
> 
> _______________________________________________
> 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