[Python-ideas] Keyword only argument on function call

Jonathan Fine jfine2358 at gmail.com
Thu Sep 6 12:40:38 EDT 2018

Hi Anders

Thank you for your interesting message. I'm sure it's based on a real
need. You wrote:

> I have a working implementation for a new syntax which would make using keyword arguments a lot nicer. Wouldn't it be awesome if instead of:
>         foo(a=a, b=b, c=c, d=3, e=e)
> we could just write:
>         foo(*, a, b, c, d=3, e)
> and it would mean the exact same thing?

I assume you're talking about defining functions.  Here's something
that already works in Python.

      >>> def fn(*, a, b, c, d, e): return locals()
      >>> fn.__kwdefaults__ = dict(a=1, b=2, c=3, d=4, e=5)
      >>> fn()
      {'d': 4, 'b': 2, 'e': 5, 'c': 3, 'a': 1}

And to pick up something from the namespace

      >>> eval('aaa', fn.__globals__)

Aside: This is short, simple and unsafe. Here's a safer way

      >>> __name__
      >>> import sys
      >>> getattr(sys.modules[__name__], 'aaa')

>From this, it should be easy to construct exactly the dict() that you
want for the kwdefaults.



More information about the Python-ideas mailing list