tags:

views:

128

answers:

4

so.. let's say i have this C function:

PyObject* Foo(PyObject* pSelf, PyObject* pArgs)
{
    MessageBox(NULL, "Foo was called!", "Info", MB_OK);
    return PyInt_FromLong(0);
}

and then, I have to do this:

static PyMethodDef Methods[] = 
{
    {"Foo", Foo, METH_NOARGS, "Dummy function"},
    {NULL, NULL, 0, NULL}
};
Py_InitModule("bar", Methods);

and I execute my python script.. but C functions are a little annoying, it's C++ and I use classes for almost everything.

So, is there any way to import member functions from a class to my python script?

oh btw, the python script looks something like this:

import bar
from bar import *
Foo()
+1  A: 

Take a look at boost python page. Search for 'free function'.

Mykola Golubyev
+5  A: 

boost.python enables you to do that very effectively.

Samuel_xL
+2  A: 

SWIG would work pretty well, too.

int3
A: 

Cython has the best C++ wrapping I have found, even though it is a bit more verbose than SWIG and it is a bit of a mindset to get into. It's easier to write mappings than SWIG -- because it uses Python types -- but you have to write them all by hand. It's also a very active project with a very friendly mailing list. It also has good buffer support for NumPy.

Matt