extending Python base class in C

harold fellermann harold.fellermann at upf.edu
Wed Jun 15 11:16:21 EDT 2005


Yahoooo! Finally, I got the thing to work. Here is how I did it.


/* import the module that holds the base class */
PyObject *base_module = PyImport_ImportModule("module_name");
if (!base_module) return;

/* get a pointer to the base class */
PyObject *base_class = PyMapping_GetItemString(
		PyModule_GetDict(base_module,"BaseClass")
	);
if (!base_class) return;

/* assign it as base class */
cclassType.tp_bases = Py_BuildValue("(O)",BaseClass);

I am doing all this before the call to PyType_Ready() -- as
Scoot Daniels suggested. Using the python API's SetAttrString()
did not work (I suppose for the same reason, why assigning
cls.__bases__ does not work in pure python either).
I also checked

cclassType.tp_base = (PyTypeObject *) base_class;

which also works and I am not aware of the difference of these two 
slots.
Anyway, for my simple purposes it works just fine.

- harold -

--
"Mr Ghandi, what do you think of western civilization?"
"Oh, this would be a great idea."




More information about the Python-list mailing list