[Python-ideas] Unpacking a dict

Rob Cliffe rob.cliffe at btinternet.com
Thu May 26 22:43:19 EDT 2016



On 26/05/2016 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
>
> which is more readable than
>
>   (partner_id, product_id, ship_to, product_ids =
>       (values[k] for k in
>           ['partner_id', 'product_id', 'ship_to', 'product_ids']))
>
> Wow.  That's a lot of room for typos and wrong order.

How about

     locals().update(values)

or if you just want a subset

     for k in ['partner_id', 'product_id', 'ship_to', 'product_ids']:
         locals()[k] = values[k]    # Look Ma, DRY


More information about the Python-ideas mailing list