#87 python manual kwargs support

Michel Zou


This is a proposal for non-automated Python kwargs arguments handling capability.

I'd be nice if swig could generate python functions with the "(self, *args, **kwargs)" argument handling instead of just "(self, *args)"
Then, using %pythonprepend, the developper has to manually check/fill/reorder the args variable to match the c++ prototypes.

In the following example, if you just replace add "**kwargs" in the generated python interface Foo constructor,
you have functional keyword argument handling:

%module example

%pythonprepend Foo::Foo(double mu) %{
if 'mu' in kwargs:
args = (kwargs['mu'],)
else if ('lb' in kwargs) and ('ub' in kwargs):
args = (kwargs['lb'], kwargs['ub'])

class Foo
Foo(double mu);
Foo(double lb, double ub);

What has to be done is to define a feature that enables to add an extra 'kwargs' argument to a function, like in our constructor:
def __init__(self, *args, **kwargs):

It would be used as:
%keywordarguments Foo::Foo(double mu)

A potential issue is when wrapping default arguments, like in
void doStuff(int a=0, int b=1) ;

where the default values are written directly in the the python interface:
def doStuff(self, a = 0, b = 1):

In that case the use of %keywordarguments would have to force to handle the default variables values
within the binary module example_wrapper.cxx instead the example.py python interface file.

What do you think ?


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks