1

Assume I have a class defined in a python module:

class A(object):
    def __init__(self):
        print 'init'
    def method(self):
        print 'method'

I'd like to instantiate an object of that class with boost::python. I tried it the following way:

namespace py = boost::python;

// importing the module and extracting its namespace to
// the variable `ns`
...

py::object a = py::exec("A()", ns)
a.attr("method")()

which prints init and then crashes. I observed that after executing

py::object a = py::exec("A()", ns)

printing the string representation of a with

std::cout << std::string(py::extract<std::string>(py::str(a))) << std::endl;

prints None. So something went wrong. How do I do this right?

1
  • I found the answer myself (but I can not answer my question myself yet, so I post it as a comment): use eval instead of exec. Commented Mar 4, 2011 at 8:27

1 Answer 1

1

I found the answer myself: use eval instead of exec.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.