[Python-ideas] Unpacking a dict

Brendan Barnwell brenbarn at brenbarn.net
Fri May 27 13:58:40 EDT 2016

On 2016-05-27 08:09, Nikolaus Rath wrote:
> 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.

	One problem with this is that currently "from" doesn't operate on names 
or objects.  It operates on module paths.  You can't do this:

import somepackage as othername
from othername import something

	(You'll get "No module named othername".)

	If we change this, it will be confusing, because when you see "from 
blah import stuff" you won't know offhand whether it's going to just 
read a value from a dict or go searching the filesystem, which are quite 
different operations.

	It might be possible, though, to leverage "from" without also having to 
use "import":

from somedict get thiskey, thatkey

	I'm not sure I actually like this idea, but I like it more than 
repurposing "import".  "import" has a quite idiosyncratic meaning in 
Python that has to do with looking for files on disk and running them; 
overloading this with totally file-local operations like getting keys 
from dicts seems too wild to me.

Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown

More information about the Python-ideas mailing list