to complete the answer from 'cpburnz', here is a function that creates a class object and adds methods:
PyObject *createClassObject(const char *name, PyMethodDef methods[])
{
PyObject *pClassName = PyUnicode_FromString(name);
PyObject *pClassBases = PyTuple_New(0);
PyObject *pClassDic = PyDict_New();
PyMethodDef *def;
for (def = methods; def->ml_name != NULL; def++)
{
printf(" add method %s\n", def->ml_name);
PyObject *func = PyCFunction_New(def, NULL);
PyObject *method = PyInstanceMethod_New(func);
PyDict_SetItemString(pClassDic, def->ml_name, method);
Py_DECREF(func);
Py_DECREF(method);
}
PyObject *pClass = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, pClassName, pClassBases, pClassDic, NULL);
Py_DECREF(pClassName);
Py_DECREF(pClassBases);
Py_DECREF(pClassDic);
return pClass;
}
:
static PyMethodDef foo_Methods[] =
{
{ "__init__", fooInit, METH_VARARGS, "doc" },
{ "do_something", fooDoSomething, METH_VARARGS, "doc" },
{ 0, 0 },
};
PyObject * fooClass = createClassObject("fooClass", foo_Methods);
PyModule_AddObject(module, "foo", fooClass );
PyModule_AddObject , "foo" Python.
: "RomanK", PyTypeObject (, , ) .
: , PyTypeObject , PyModule_AddObject(), Python. , . , : https://docs.python.org/3.3/extending/newtypes.html?highlight=pytypeobject