[C++-sig] boost python and keyword aguments

Moe Goldberg goldberg at coredp.com
Tue May 13 17:03:01 CEST 2008


Thanks! That's pretty much what I am looking for.

The docuemntation says, you can write a function this way
tuple raw(tuple args, dict kw)
{
return make_tuple(args, kw);
}

Which is similar to
raw(*args, **kwargs):
return (args, kwargs)
in pure python.

Is there a way to write a C++ function with set args, but still use 
kwargs ... such as:
myfunc(arg1, arg2, arg3, **kwargs):
do_something()

I tried to do this with raw_function, and got a compilation error.
tuple myfunc(int arg1, int arg2, int arg3, dict kw)
{
return make_tuple(arg1, arg2, arg3, kw);
}

thanks again for your help.


Nikolay Mladenov wrote:

> Hi Moe,
>
> look at this: 
> http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/raw_function.html
>
>
> HTH,
>
> Nikolay Mladenov
>
> Moe Goldberg wrote:
>
>> If I'm writing pure python, I can do the following:
>>
>> def pyfunc(**kwargs):
>> print kwargs
>>
>> then call it as follows:
>> >>> pyfunc()
>> {}
>>
>> >>> pyfunc(a="b", c="d", arbitrary_key="value")
>> {'a': 'b', 'c': 'd', arbitrary_key: 'value'}
>>
>>
>> Is there a way with boost python to do the same thing? i.e. write a 
>> function in C++ something like:
>> void cppfunc(boost::python::dict kwargs)
>> {
>> // ... do something ...
>> }
>>
>>
>> That can be called in python like this:
>> >>> cppfunc(a="b", c="d", arbitrary_key="value")
>>
>> The important thinhg is that I be able to call it with keyword args, 
>> and not by passing a dict
>>
>> Thanks
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
>



More information about the Cplusplus-sig mailing list