[Python-ideas] Keyword-only arguments?

Carl Meyer carl at oddbird.net
Wed Jun 17 21:02:03 CEST 2015


Hi Amber,

On 06/17/2015 12:58 PM, Amber Yust wrote:
> One thing that has been a source of bugs and frustration in the past is
> the inability to designate a named keyword argument that cannot be
> passed as a positional argument (short of **kwargs and then keying into
> the dict directly). Has there been any previous discussion on the
> possibility of a means to designate named arguments as explicitly
> non-positional?
> 
> Not a solid proposal, but to capture the essential difference of what
> I'm thinking of, along the lines of...
> 
>     def foo(bar, baz=None, qux: None):
> 
> where bar is a required positional argument, baz is an optional argument
> that can have a value passed positionally or by name, and qux is an
> optional argument that must always be passed by keyword.
> 
> Such a means would help avoid cases where a misremembered function
> signature results in a subtle and likely unnoticed bug due to unintended
> parameter/argument mismatch.
> 
> (It's possible that this has been discussed before - a cursory search of
> python-ideas didn't bring up any direct discussion, but I may have
> missed something. If you have a link to prior discussion, please by all
> means point me at it!)

I can do better than prior discussion - this already exists in Python 3:

Python 3.4.2 (default, Dec 12 2014, 17:46:08)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(bar, *, baz=None):
...     print(bar, baz)
...
>>> foo('a', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes 1 positional argument but 2 were given
>>> foo('a', baz='b')
a b

See https://www.python.org/dev/peps/pep-3102/

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150617/5fd49fff/attachment-0001.sig>


More information about the Python-ideas mailing list