9. Python C Extensions
These examples demonstrate creating Python extensions in C for tasks requiring performance optimizations, such as mathematical computations, array processing, and string manipulations.
#include <Python.h>
static PyObject* hello_world(PyObject* self, PyObject* args) {
printf("Hello, World!\n");
Py_RETURN_NONE;
}
static PyMethodDef HelloMethods[] = {
{"hello_world", hello_world, METH_VARARGS, "Prints Hello, World!"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef hellomodule = {
PyModuleDef_HEAD_INIT,
"hello",
NULL,
-1,
HelloMethods
};
PyMODINIT_FUNC PyInit_hello(void) {
return PyModule_Create(&hellomodule);
}Last updated