I am trying to learn how to extend python using C extensions and so far I have been able to go through the official python docs for the same.
Browsing through, I have found this helpful resource which explains how to extend python using C extensions. So, the example looks like so:
#include <Python.h>
int
_fib(int n)
{
if (n < 2)
return n;
else
return _fib(n-1) + _fib(n-2);
}
static PyObject*
fib(PyObject* self, PyObject* args)
{
int n;
if (!PyArg_ParseTuple(args, "i", &n))
return NULL;
return Py_BuildValue("i", _fib(n));
}
static PyMethodDef FibMethods[] = {
{"fib", fib, METH_VARARGS, "Calculate the Fibonacci numbers."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initfib(void)
{
(void) Py_InitModule("fib", FibMethods);
}
However, I am having trouble in understanding what this piece of code does:
int
_fib(int n)
{
if (n < 2)
return n;
else
return _fib(n-1) + _fib(n-2);
}
especially, the _
part of the function name.
I would really appreciate if someone could explain what the above piece of code does.