Function Parameters

Ian Kelly ian.g.kelly at
Thu Dec 27 21:33:45 CET 2012

On Thu, Dec 27, 2012 at 1:16 PM, Joseph L. Casale
<jcasale at> wrote:
> When you use optional named arguments in a function, how do you deal with with
> the incorrect assignment when only some args are supplied?
> If I do something like:
>     def my_func(self, **kwargs):
> then handle the test cases with:
>     if not kwargs.get('some_key'):
>         raise SyntaxError
> or:
>     if kwargs.get('some_key') and kwargs.get('another_key'):
>         ...
> I loose the introspection that some IDE's provide from the doc strings.
> Any ideas on how to deal with this?

Don't use kwargs for this.  List out the arguments in the function
spec and give the optional ones reasonable defaults.

def my_func(self, some_key=None, another_key=None):
    if some_key and another_key:

If None is a meaningful value for the argument, then a good technique
is to use a unique object as the default instead.

MISSING = object()

def my_func(self, some_key=MISSING, another_key=MISSING):
    if some_key is not MISSING and another_key is not MISSING:

I only use kwargs myself when the set of possible arguments is dynamic
or unknown.

More information about the Python-list mailing list