[C++-sig] Callable object instance as object method.

Kerim Borchaev warkid at hotbox.ru
Thu Aug 14 17:05:06 CEST 2003


Hello!

I want to use a C++ callable object instance as python object method:

//callable_as_method.cpp/////////////////////////////////
struct C{
};

struct Callable{
    void call(C& c){
    }
};

#include <boost/python/class.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

BOOST_PYTHON_MODULE(callable_as_method)
{
    using namespace boost::python;
    class_<Callable>("Callable")
        .def("__call__", Callable::call);
    class_<C> c("C");
    c.setattr("method", Callable());
}
//////////////////////////////////////////////////////////

And when I run this test:

#test_callable_as_method.py###############
import test
c = test.C()
c.method(c)
c.method()
##########################################

I get this result:

Traceback (most recent call last):
  File "test_callable_as_method.py", line 5, in ?
    c.method()
TypeError: bad argument type for built-in operation

That's (apparently) happens because 'method' is exported not as a
function.
Is it possible to export it in such a way that C().method whould
result in bound method object?

Best regards,
 Kerim                          mailto:warkid at hotbox.ru






More information about the Cplusplus-sig mailing list