开发者

Import C++ classes in python?

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.. b开发者_开发技巧ut 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()


boost.python enables you to do that very effectively.


SWIG would work pretty well, too.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜