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 Answers1

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().

Community
  • 1
  • 1
Mike
  • 86
  • 3