[C++-sig] Calling python from c++ with tuple and dict args.

Alex Mohr amohr at pixar.com
Tue Mar 7 18:48:37 CET 2006


Thanks Markus,

I was planning to use PyObject_Call which takes a tuple and a dict if I 
couldn't find a nice boost python way to do it.

It seems like it would be possible to do something like this:

bp::object::operator()(bp::unpacked_tuple const &args,
                        bp::unpacked_dict const &kw) {
     // call as self(*args, **kw)
}

And then define unary * operators for tuple and dict so that *tuple 
would produce an unpacked_tuple and **dict would produce an unpacked_dict.

Alex


Markus Plessing wrote:
>>Hi folks,
> 
> 
> Hi Alex,
> 
> 
>>[...] 
>>Is there a convenient boost python way to call the python object f like 
>>this from C++?
> 
> 
> This may help. If not take a look at [1]
> 
> ------------------------------------------------------------------------
> PyObject* PyObject_CallObject
> 	(PyObject *callable_object, PyObject *args)
> 
>     Return value: New reference.
> 
> Call a callable Python object callable_object, with arguments given 	
> by the tuple args. If no arguments are needed, then args may be NULL.
> Returns the result of the call on success, or NULL on failure. This is
> the equivalent of the Python expression "apply(callable_object, args)"
> or "callable_object(*args)".
> -------------------------------------------------------------------------
> 
> 
>>Thanks,
>>
>>Alex
> 
> 
> Kind regards,
> 
> Markus
> 
> [1] http://docs.python.org/api/object.html#l2h-241
> 



More information about the Cplusplus-sig mailing list