[C++-sig] instantiate python classes in c++

Gregor Burger Gregor.Burger at uibk.ac.at
Mon Mar 30 17:55:34 CEST 2009

hi all,

is it possible to use boost.python to define classes
in a script (like in the one below), register the defined
classes and later on create instances of the class?

from cd3 import *

class MyNode(Node):
	def init(self, start, stop, dt):
		in_flow = Flow()

	def f(self, time, dt):
		return dt


registering of the class is working also instantiating
the class. but it seems that when i call init(...) and f(...)
afterwards the namespace and all imports are lost. so
in_flow = Flow() raises an global name Flow undefined. The wrapping
of Flow works on first execution.

Is it possible to provide the same globals and locals to call_method
so that Flow and other classes are defined?

thanks in advance.

gregor burger
university of innsbruck

the wrapped Node looks like this:

struct NodeCallback : Node {
	NodeCallback(PyObject *p) : self(p) {

	virtual ~NodeCallback() {

	void init(int start, int stop, int dt) {
			call_method<void, int, int, int>(self, "init", start, stop, dt);

	int f(int time, int dt) {
		return call_method<int, int, int>(self, "f", time, dt);

	void addInPort(object o) {
		std::string name = extract<std::string>(o.attr("__name__"));
		std::cout << "adding in port " << name << std::endl;

	PyObject *self;

More information about the Cplusplus-sig mailing list