API class creation

Daniel Dittmar daniel.dittmar at sap.corp
Thu Aug 4 05:36:31 EDT 2005


kman3048 wrote:
> Hello,
> 
> as a relative newcomer to Python API programming I've got a problem:
> 
> To extend Python:
> - there is an API C call to create a module
> - there is also a API C call to create a method
> - there is an API C call to create a Class instance
> 
> Now, I need to create a Class and fill it with Methods and Variables.
> There are means to create (and attache) methods and variables. 
> However, I have not found how to create a Class within a Module. Or do 
> I have to use a low level API function to allocate an Object from Heap?

static PyMethodDef moduleMethods [] = {
     ...
};

statichere PyTypeObject MyClassType = {
     PyObject_HEAD_INIT (NULL)
     ...
};

initmymodule ()
{
     PyObject* module;
     PyObject* dict;

     module = Py_InitModule4 ("mymodule", moduleMethods,
           "doc string", NULL, PYTHON_API_VERSION);
     if (module == NULL) {
         return;
     }
     dict = PyModule_GetDict (module);
     PyDict_SetItemString (dict, "MyClass"),
         (PyObject*) &MyClassType));
}

Daniel



More information about the Python-list mailing list