[Python-ideas] Unpacking a dict

Ethan Furman ethan at stoneleaf.us
Fri May 27 12:51:07 EDT 2016


On 05/27/2016 08:09 AM, Nikolaus Rath wrote:
> On May 26 2016, Paul Moore wrote:
>> On 26 May 2016 at 18:55, Ethan Furman wrote:

>>> With the simple syntax that I could live with, a real example could be:
>>>
>>>    {active_id, active_ids, active_model} = context
>>>
>>> or
>>>
>>>    {partner_id, product_id, ship_to, product_ids} = values
>>
>> The behaviour of using the names of the variables from the LHS to
>> introspect the value on the RHS is, to me, extremely magical and
>> unlike anything I've seen in any other language. I don't think it sits
>> well in Python, even though it is certainly a very readable idiom for
>> the sort of unpacking we're talking about here.
>
> Very true. But as someone else already said (I can't find the email
> right now), we have a different construct that everyone is familiar with
> and that's easily adapted for this situation:
>
> from dict context import active_id, active_ids, active_model
>
> or more general:
>
> "from dict"  <expr> "import" <identifier list>
>
> Everyone knows that "from .. import .." modifies the local namespace. We
> just have to extend it to work not just on modules, but also on
> dictionaries.

-1

import works with modules.  Having it work with other things would muddy 
the concept, plus make module/object naming conflicts an even bigger hassle.

--
~Ethan~



More information about the Python-ideas mailing list