[Python-ideas] Unpacking a dict
leewangzhong+python at gmail.com
Tue May 31 15:58:09 EDT 2016
On Tue, May 31, 2016 at 1:22 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> 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
As two words:
fromdict <expression evaluating to dict> import <valid
identifier>[ as <valid identifier>][, <valid identifier>[, ...]]
- The desired feature really is more like import than unpacking. It's
only different in that one is a lookup to d.__getitem__ while the
other is a file lookup, then a load, then (I believe) a lookup to
- Don't repeat the key.
- The names being introduced should be syntactic tokens, [so that ||
because] the compiler knows to make cells in locals() for those names.
- The mapping should be named first. (TBF, I also think that, in
comprehensions, the item should be named before the expression using
it, so maybe my senses are wrong.)
- The syntax should make it obvious that a dict-like object is
expected, even to those who haven't seen it before. Leading `extract`
and `export` don't satisfy that. (`extract key` makes me itch when I
see more than one key follow, and it isn't clear without reading
further that "key" isn't what's being extracted.)
- As long as it is clear from the start of the line whether it is a
module or a dict being imported, it's okay to use `import`.
It's too bad "fromdict" is used in the wild
and "frommapping" is too long (in letters, syllables, and constituent
parts: "from", "map", and "-ing").
`fromdict d import *`:
People might expect this, for symmetry with `from ... import *`.
In CPy, there's no sense of dynamically-created local names (since
locals are looked up in the compiler), and you couldn't use them
anyway. (We don't talk about `eval` and `exec`.) In global scope,
`import *` makes more sense, but I think you can just say, "If you
need something like that, you should've created a module instead."
Possible (gross) extensions:
fromdict d import 'literalstring' as name2, (evaluatedname) as name2
More information about the Python-ideas