1

I have a module with a C++ class exposed:

class MyClass{
public:
    MyClass(){}
    void foo(){
        //...
    }
};

BOOST_PYTHON_MODULE(my_module){
    class_<MyClass>("MyClass", init<>())
        .def("foo", &MyClass::foo)
        ;
}

In Python, I create a class that is derived from this class.

from my_module import *

class Derived(MyClass):
    def __init__(self):
        self.foo()

d = Derived()

I get the error:

Boost.Python.ArgumentError: Python argument types in
    MyClass.foo(Derived)
did not match C++ signature:
    foo(class MyClass {lvalue})

I expected to be able to call a function defined in the base class from the derived class. How can I fix this?

1 Answer 1

3

Looks like you need to call MyClass.__init__(self) before calling foo; otherwise foo isn't getting a MyClass instance. Bonus points for using super().

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.