
On 29/06/2013 17:56, Steven D'Aprano wrote:
On 30/06/13 02:20, Joshua Landau wrote:
We*do* have a way of extracting a submap from a dict.
Er, not really. Here's a dict: {}
What you talk about is extracting named attributes from a module or package namespace. That modules happen to use a dict as the storage mechanism is neither here nor there. I can do this:
class NoDict(object): __slots__ = ['eggs']
fake_module = NoDict() fake_module.eggs = 42 assert not hasattr(fake_module, '__dict__')
sys.modules['spam'] = fake_module
from spam import eggs
and it all just works. But I'm no closer to extracting a subdict from a dict.
We*already* have a way of writing
foo = foo
across scopes, as we want to do here.
We sure do. And that's by just explicitly writing foo=foo. This is not a problem that needs solving. I'm kind of astonished that so many words have been spilled on a non-problem just to save a few characters for such a special case.
This problem*has* been solved before, and it looks like:
from module import these, are, in, the, submap
As I show above, this is about named attribute access, not dicts. And as you admit below, it doesn't lead to good syntax for extracting a subdict:
I haven't been able to find a really good syntax from this, but something like:
1) foo(a, b, **(key1, key2 from locals()))
2) {**(key1, key2 from locals())}
3) import key1, key2 from {"key1": 123, "key2": 345}
etc.
You could just add a method to dict:
foo(a, b, **locals().subdict(["key1", "key2"]))
And, รก mon avis, it's a hell of a lot better than previous proposals for (1) and (2) from this thread, and (3) from other threads.
Again, not a proposal per se but a revelation.