[Python-ideas] Short form for keyword arguments and dicts

Joshua Landau joshua.landau.ws at gmail.com
Sat Jun 29 21:48:28 CEST 2013


On 29 June 2013 17:56, Steven D'Aprano <steve at pearwood.info> 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.

I realise you used "slots", but it's still a dictionary. Well, OK,
it's merely a "mapping" but I thought we duck-typed.
So that's what you just did. You took a part of a namespace and added
it to another namespace. That's what we wanted to do with:

    function_call(pass "arg" from local namespace to function's namespace)

The fact that Python's namespaces are dictionaries isn't irrelevant.
They're the same thing, and I think it's foolish to claim that's
purely by implementation. Dictionaries are mappings are namespaces;
they provide the *same* functionality through the same interface with
slightly different semi-arbitrary restrictions and APIs.

Just imagine my post but using "namespace" instead of "dictionary" and
you might get the point.

>> 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.

As has been shown, this is actually a problem that quite a lot of
people care about. There are a lot of threads (by far not just this
one) that have understood that moving an argument from one namespace
to another is something that feels like it should be able to look
nicer.

Surely you'd understand the problem if we had to write:

module = import("module")
arg = module.arg
foo = module.foo
blah = module.blah
...

which, as I was saying, is *exactly the same problem* -- except that
we've already solved it.

That was what I'd realised and what I was pointing out.


The rest of the post is along these lines, so which I've covered, and
about the syntax, which is beside the point.


More information about the Python-ideas mailing list