Python/C API Inheritance

  • Thread starter Евгений Почитаев
  • Start date
Ð

Евгений Почитаев

Hi to all.
May be someone do class inheritance in Python/C API, I know how create
superclass/subclass. But how I can initialize superclass from subclass
initializer?

struct SuperClass {
PyObject_HEAD;
//...
};

struct SubClass {
PyObject_HEAD;
//...
};

static int SubClassInit(SubClass *self, PyObject *args, PyObject
*kwds)
{
// how I should initialize super class?
}

PyTypeObject SuperClassType = {
//...
};

PyTypeObject SubClassType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size*/
//...
SubClassInit, /* tp_init */
//...
NULL, /* tp_getset */
&SuperClassType, /* tp_base */
NULL, /* tp_dict */
//...
};

Another question how I can access to superclass from subclass(I need
get access to SuperClass C struct members)?

Thanks.
 
S

Stefan Behnel

Евгений Почитаев, 02.02.2011 18:21:
May be someone do class inheritance in Python/C API, I know how create
superclass/subclass. But how I can initialize superclass from subclass
initializer?

struct SuperClass {
PyObject_HEAD;
//...
};

struct SubClass {
PyObject_HEAD;

You need to insert the super class struct here, not a plain new object struct.

In case you want to avoid stumbling over problems like this and instead
concentrate on getting functionality implemented, take a look at Cython.

http://cython.org

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top