API class creation
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? One possible solution I think is not very elegant: - define a module and class within Python scripting - use this object by creating Instances of this object via API Is there no better way (however, I don't know if above works anyway). Thanks, Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: API class creation
Thanks, that helped a lot. Anybody who wants to attempt the same might find the source code tree of Python i.e. file src/Modules/cdmodule.c helpful. --Matt Daniel Dittmar wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list
adding standard types to new type
I've created a new type using the API and want to add not only methods (which is easy with PyMethodDef structure) but also insert several Python standard type variables. Is there some equivalent to PyModule_AddObject - just working on the new type instead Module? Or do I have to add a dictionary to the new type and insert the new variables to the dictionary? What is the correct way? Using the new type should be: import NewTypeModule newtypevar=NewTypeModule.create() dir(newtypevar) <'method1', 'method2', 'variable1', ...> type(newtypevar.variable1) <'tuple'> Thanks a Lot! Matt -- http://mail.python.org/mailman/listinfo/python-list