[Python-ideas] Unpacking a dict
Nikolaus Rath
Nikolaus at rath.org
Wed Jun 1 23:23:21 EDT 2016
On May 31 2016, Paul Moore <p.f.moore-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
> On 31 May 2016 at 20:57, Nikolaus Rath <Nikolaus-BTH8mxji4b0 at public.gmane.org> wrote:
>> On May 31 2016, Paul Moore <p.f.moore-Re5JQEeQqe8AvxtiuMwx3w-XMD5yJDbdMReXY1tMh2IBg at public.gmane.org> wrote:
>>> 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...
>>
>> How about
>>
>> unravel query_result (this, that, something)
>
> Meh. Don't assume that if you come up with a good word, I'll be in
> favour. At best, I'll go from -1 to -0 or maybe +0. Should we get to
> something that looks reasonably attractive to me, there's still issues
> with the whole thing being a niche problem, limited applicability (by
> leaping at "unravel" you lost the ability to extract attributes from
> an object - did you mean to do that?), etc.
>
> Basically, don't waste too much time trying to convince me. A better
> bet would be to get sufficient support from others that my opinion is
> irrelevant (which it may well be anyway :-))
Nah, I think I don't like my own idea anymore. I think the whole issue
is much better addressed by using an assignment with a placeholder on
the LHS. This also solves the DRY problem when creating things like
namedtuples:
d = query_result
product_id = q[$lhs]
quantity = q[$lhs]
distributor = q[$lhs]
description = q[$lhs]
# or
my_favorite_named_tuple = namedtuple($lhs, "foo bar com")
$lhs would be a special construct that is only allowed in the RHS of an
assignment statement and evaluates to a string representation of the
identifier on the LHS.
But since I don't have time to learn how to extend the Python parser, I
will shut up about this now.
Best,
-Nikolaus
--
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F
»Time flies like an arrow, fruit flies like a Banana.«
More information about the Python-ideas
mailing list