Notes taking on the subject of C extension with Python 3
Notes are taking on the video called Python3 Advanced Tutorial 9
{ NULL, NULL, 0, NULL }
Example :
static PyMethodDef myMethods[] = {
{"func1", func1, METH_NOARGS, "Func1 doc"},
{"func2", func2, METH_NOARGS, "Func2 doc"},
{ NULL, NULL, 0, NULL }
}
Pattern used: pyMethodName
, function
, functionType
, PyDocs
PyModule_Create()
function what information to use to create the moduleFibonnaci Example:
#include <Python.h>
int Cfib(int n){
if (n < 2)
return n;
else
return Cfib(n -1) + Cfib(n -2);
}
static PyObject* fib(PyObject* self, PyObject* args){
int n;
if(!PyArg_ParseTuple(args, "i", &n))
return NULL;
return Py_BuildValue("i", Cfib(n));
}
static PyObject* version(PyObject* self){
return Py_BuildValue("s", "Version 1.0");
}
static PyMethodDef myMethods[] = {
{"fib", fib, METH_VARARGS, "Calculate the fibonacci numbers."},
{"version", (PyCFunction)version, METH_NOARGS, "Returns the version"},
{NULL. NULL, 0, NULL}
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Fibonacci Module",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
return PyModule_Create(&myModule);
}