[Python-ideas] Generator unpacking

Ethan Furman ethan at stoneleaf.us
Mon Feb 15 12:39:34 EST 2016


On 02/15/2016 06:42 AM, Steven D'Aprano wrote:

> py> [] = "abc"
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> ValueError: too many values to unpack
>
>
> There are three values on the right hand side, and zero targets.
>
> This might even be useful. You can confirm that an iterable is empty
> (why you might want to do this, I can't imagine, but suppose you did) by
> assigning it to zero targets:
>
> [] = iterable
>
> succeeds only if iterable has zero items, otherwise it raises
> TypeError.

I /think/ I've used that trick to confirm an iterable was empty; I 
/know/ I've used

[some_var] = some_var

to extract the only item in a one-item list (and verify it had, in fact, 
only one item).

--
~Ethan~



More information about the Python-ideas mailing list