[C++-sig] Function handling with Boost

Christopher Schramm cschramm at shakaweb.org
Tue Jun 23 22:03:16 CEST 2009


Stefan Seefeld wrote:
> I read your original mail, but I didn't understand what you are trying
> to achieve. You certainly can export functions to python:
> 
> void foo(...);
> 
> ...
> 
> bpl::def("foo", foo);
> 
> 
> works just fine. There shouldn't be any need to wrap the function in a
> class (member function), and then create some hidden object to invoke
> it. Or am I misunderstanding what you want ?

The problem is, I'm not writing a module that shall be imported, but I'm
embedding Python and want to provide a special "built in" module so the
final result is a single monolithic binary internally starting the
interpreter, creating the module and running user defined scripts.

Since boost::python's def function has a return type of void (sure,
because there seems to be no type for python functions in bpy),
functions can only be exported within a BOOST_PYTHON_MODULE (which I
don't have) or when belonging to a class (which led to my workaround).

>> global["my_mod"].attr("__dict__")["f1"] = tmp.attr("__dict__")["f1"];
>>
>
> Why not simply
>
> global["my_mod"]["f1"] = ... ; ?

Does that have the same effect? So I don't have to use the "__dict__"
attribute? Didn't know that. As I said, I'm completely new to Python's C
API and Boost Python. I'll try that, thanks for the tip.


More information about the Cplusplus-sig mailing list