#include //<-Put this FIRST! static int _fact(int v) { if (v<2) return 1; return v*_fact(v-1); } static PyObject* factorial(PyObject *self, PyObject *args) { int value; //Try parsing the argument tuple //https://docs.python.org/2/c-api/arg.html if (!PyArg_ParseTuple(args,"i",&value)) return NULL; //<-Something didn't work! return Py_BuildValue("i",_fact(value)); //An appropriate Python object/tuple is constructed for return } //If it ain't here, it ain't in the module //Always terminate with {NULL,NULL,0,NULL} static PyMethodDef toolsMethods[]={ {"fact",factorial,METH_VARARGS,"Calculate a factorial!"}, {NULL,NULL,0,NULL} }; PyMODINIT_FUNC inittools(void) { (void) Py_InitModule("tools",toolsMethods); //There are other Init's, including one that lets you have a docstring //(void) Py_InitModule3("tools",toolsMethods,"Handy-dandy tools!"); }