[Python-ideas] Generator unpacking

Georg Brandl g.brandl at gmx.net
Tue Feb 16 12:54:21 EST 2016


On 02/16/2016 11:07 AM, Sven R. Kunze wrote:
> On 16.02.2016 00:43, Steven D'Aprano wrote:
>> No, it's exactly the same Python idiom (assignment to a list of targets)
>> as we've been talking about for the last few posts.
> 
> I think we better distinguish between idioms and language features.
> 
>> We've had examples
>> with four targets, three targets, two targets and zero targets. This is
>> an example with one target.
>>
>> [a] = iterable
>>
>> requires that the right-hand side be iterable, and after unpacking it
>> must contain exactly one item, to match the one assignment target given
>> on the left.
> 
> Of course, it's quite straightforward once you ponder about it. I 
> recently talked to a coworker about this. The concrete example is about 
> "How do I get the one-and-only element of a **set** which obviously does 
> not support subscripting".
> 
> 
> Another aspect, I came to think of is the following asymmetry:
> 
> a, b, c, d = mylist4 # works
> a, b, c = mylist3    # also works
> a, b = mylist2       # works too
> [a] = mylist1        # special case?

a, = mylist1

actually.  Same "special case" as with 1-element tuples.

Georg




More information about the Python-ideas mailing list