Brammie
Brammie

Reputation: 610

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

Upvotes: 2

Views: 5616

Answers (4)

Matt
Matt

Reputation: 747

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.

Upvotes: 0

int3
int3

Reputation: 13201

SWIG would work pretty well, too.

Upvotes: 2

KeatsPeeks
KeatsPeeks

Reputation: 19337

boost.python enables you to do that very effectively.

Upvotes: 5

Mykola Golubyev
Mykola Golubyev

Reputation: 59844

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

Upvotes: 1

Related Questions