[Python-ideas] Unpacking a dict

Paul Moore p.f.moore at gmail.com
Tue May 31 13:22:24 EDT 2016


On 31 May 2016 at 17:10, Nikolaus Rath <Nikolaus at rath.org> wrote:
> On May 30 2016, Paul Moore <p.f.moore-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
>> On 30 May 2016 at 16:07, Nikolaus Rath <Nikolaus-BTH8mxji4b0 at public.gmane.org> wrote:
>>> Yeah, but that might be useful too :-). How about:
>>>
>>> extract key foo from d
>>> extract attribute foo import d
>>>
>>> or
>>>
>>> export key foo from d
>>> export attribute foo import d
>>>
>>> As for "import", with both foo and d required to be identifiers.
>>
>> At this point, the question has to be, how is this any better than
>>
>> foo = d.foo
>> foo = d['foo']
>
> Huh? I thought this has been discussed at length. It's not better in toy
> examples, but consider this:
>
> r = query_result
> product_id = r['product_id']
> quantity = r['quantity']
> distributor = r['distributor']
> description = r['destription']
>
>
> Compare to
>
> export key (product_id, quantity, distributor,
>             description) from query_result
>

Not saying the current idiom can't be improved, but I don't like this
approach - it seems to be focused on compressing the extraction into
one line, which isn't a benefit to me I typically find it easier to
line up, and scan, information vertically rather than horizontally. So
the "export" approach for me would need to be written

export key (
    product_id,
    quantity,
    distributor,
    description
) \
from query_result

That backslash after the close bracket is ugly. But not having a
"blank" line before the "from query_result" line is also difficult to
read. The list of repeated assignments is too repetitive, but
nevertheless can be lined up more neatly.

Technically, the "export key" approach could probably me made readable
in a way I'd be OK with, for example:

export key from query_result (
    product_id,
    quantity,
    distributor,
    description
)

but I'm not at all sure that needing 2 new keywords ("export" and
"key") and a reuse of an existing one ("from") is going to fly - it's
too wordy, feels like SQL or COBOL to me. Maybe if someone comes up
with a one-word option for "export key from" then it would be
viable...

Paul


More information about the Python-ideas mailing list