[BangPypers] Enforcing keyword only arguments

Praveen Shirali praveengshirali at gmail.com
Wed Aug 10 17:12:03 EDT 2016

IMO, No.

With Py2, the caller has the freedom to positionally pass values against
the kwargs (and with py3 as well, if you remove the '*' after positional
args). Hence, if your API is forcing the caller to pass some values only
via kwargs, then that's a restriction that's non-obvious and deviates from
the general pattern. As the API provider, you need to define known and
unknown arguments (positional or keyword) and trust that the caller knows
how to invoke them properly.

It is common to use kwargs.pop("key", <default>) in __init__ of derived
classes, where your derived class consumes a few arguments of your own, and
you'd like to pass the remainder of **kwargs to the baseclass's __init__
via super(). This is where you absolve yourself from having to redeclare
parent's arguments in the derived class's __init__ (which is a big relief,
and outweighs other negatives related to documentation of args).

If you wish for `my_func` to be equivalent in behaviour in py2 and py3, you
should include **kwargs in your py3's function signature.
However, IMO its better to completely avoid enforcing kwargs usage on the


On 11 August 2016 at 01:30, Sudheer Satyanarayana <sudheer.zzz at sudheer.net>

> Hello BangPypers,
> Python 3 has this nifty feature of including the * in the argument list to
> indicate the end of positional arguments and the beginning of keyword-only
> arguments.
> For example:
> def my_func(a, b, *, kw1=None, kw2=None): pass
> In Python 2 you could achieve the same goal of enforcing keyword-only
> arguments using a technique like below example function:
> def my_func(a, b, **kwargs):
>     kw1 = kwargs.pop('kw1', None)
>     kw2 = kwargs.pop('kw2', None)
> Would you consider this technique of enforcing keyword-only argument in
> Python 2 idiomatic?
> I understand that it could be a matter of personal taste. But I'm curious
> to know what other Python programmers think about it.
> -
> Sudheer Satyanarayana
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers

More information about the BangPypers mailing list