[Python-ideas] Generator unpacking

Steven D'Aprano steve at pearwood.info
Fri Feb 12 07:46:55 EST 2016


On Fri, Feb 12, 2016 at 07:09:38AM -0500, Edward Minnix wrote:
> 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.

That's because count() is an infinite generator.



> Would there be a way to add generators to the unpackables, even if it was
> only in the front?

Generators can already be unpacked. They just have to be finite:

py> def gen():
...     yield 1
...     yield 2
...     yield 3
...     yield 4
...
py> a, b, *c = gen()
py> a
1
py> b
2
py> c
[3, 4]


I'm surprised that the unpacking can't be interrupted with Ctrl-C. I 
think that is a bug.


-- 
Steve


More information about the Python-ideas mailing list