question about using a python object from C++
hello, I've spent a lot of time reading the Boost web pages, and I'm still mystified. I have a class that I built in Python. I want to use this class in C++. From reading about Boost it seems to allow bidirectional operation, but I can't find a simple (or any) example of using C++ to access a Python object. Could someone please point me in the right direction? Further digging has led me to the "embedding Python" pages of the main python docs, but that seems quite complicated for C++, and I'd imagine simplifying that is one of the things Boost.Python does. So is there a simple example somewhere of how to do this: In Python I have defined the class "myclass", and it has methods "Init", "Run" & "Shutdown" Ideally, in C++ I want to be able to do something like: <include "myclass.h"> ... myclass obj; obj.Init(initdata); obj.Run(params); obj.Shutdown(); thanks in advance for any help on this, Shi.
--- Shi Sherebrin <shi@imaging.robarts.ca> wrote:
I've spent a lot of time reading the Boost web pages, and I'm still mystified. I have a class that I built in Python. I want to use this class in C++. From reading about Boost it seems to allow bidirectional operation, but I can't find a simple (or any) example of using C++ to access a Python object. Could someone please point me in the right direction?
I am confused by your "ideal" example, but maybe this is useful: Given sandbx/matrix.py: class matrix: def __init__(self, (n_rows, n_columns)): self.n = (n_rows, n_columns) Attached is the code that shows how you can create a sandbx.matrix.matrix instance in C++. This is the test: from sandbx_boost import to_matrix m = to_matrix.to_matrix() assert m.n == (2,3) Ralf P.S.: Sorry for all the namespace stuff. It is not essential. This is just what I have. #include <boost/python/module.hpp> #include <boost/python/def.hpp> #include <boost/python/tuple.hpp> namespace sandbx { namespace { boost::python::handle<> import_module(const char* module_name) { using namespace boost::python; return handle<>(PyImport_ImportModule(const_cast<char*>(module_name))); } boost::python::object to_matrix() { using namespace boost::python; object matrix_module(import_module("sandbx.matrix")); object matrix = matrix_module.attr("matrix"); return matrix(make_tuple(2,3)); } void init_module() { using namespace boost::python; def("to_matrix", to_matrix); } }} // namespace sandbx::<anonymous> BOOST_PYTHON_MODULE(to_matrix) { sandbx::init_module(); } __________________________________ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com
Shi Sherebrin <shi@imaging.robarts.ca> writes:
hello,
I've spent a lot of time reading the Boost web pages, and I'm still mystified. I have a class that I built in Python. I want to use this class in C++. From reading about Boost it seems to allow bidirectional operation, but I can't find a simple (or any) example of using C++ to access a Python object. Could someone please point me in the right direction?
Getting ahold of the Python class object in C++ is your first problem. The easiest way is to pass it as an argument to a wrapped C++ function: void f(boost::python::object python_class) { ... } Now you can construct an instance of the class just as you would in Python: void f(object python_class) { object instance = python_class(3, "hello", 22.5); instance.Run(42) instance.Shutdown(); ... } >>> class Foo: ... def __init__(count, message, rating): ... self.count = count ... self.message = message ... self.rating = rating ... >>> import my_extension >>> my_extension.f(Foo) If you can't pass the class into your C++ function, you'll have to use the Python 'C' API to access the module in which you've created it and pull out the class attribute: // Retrieve the main module // Have to use the Python 'C' API for this part unfortunately. object main_module( python::handle<>( python::borrowed(PyImport_AddModule("__main__")) )); // get __main__.Foo object Foo = main_module.attr("Foo"); // instantiate one object inst = Foo(3, "hello", 22.5); // use it. inst.Run(42) inst.Shutdown(); -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams -
Ralf W. Grosse-Kunstleve -
Shi Sherebrin