Syntactic sugar for assignment statements: one value to multiple targets?

MRAB python at mrabarnett.plus.com
Tue Aug 16 22:11:36 EDT 2011


On 17/08/2011 01:14, gc wrote:
> On Aug 16, 4:39 pm, "Martin P. Hellwig"<martin.hell... at gmail.com>
> wrote:
>> On 03/08/2011 02:45, gc wrote:
>> <cut>
>>
>>> a,b,c,d,e = *dict()
>>
>>> where * in this context means something like "assign separately to
>>> all.
>
>> <snip>  . . . it has a certain code smell to it.<snip>
>> I would love to see an example where you would need such a construct.
>
> Perfectly reasonable request! Maybe there aren't as many cases when
> multiple variables need to be initialized to the same value as I think
> there are.
>
[snip]
As I see it, there are 2 issues:

1. Repeated evaluation of an expression: "dict()" would be evaluated as
many times as necessary. In other words, it's an unlimited generator.

2. Lazy unpacking: unpacking normally continues until the source is
exhausted, but here you want it to stop when the destination (the RHS)
is satisfied.

It just happens that in your use-case they are being used together.



More information about the Python-list mailing list