Creating an array squared function using Python C-API
The Python function passes a reference to itself as the first argument, followed by real arguments given to the function. The
PyArg_ParseTuple function is used to parse values from the Python function to local variables in the C function. In this function, we cast a value to a double, and hence we use
d as the second argument. You can see a full list of strings that are accepted by this function at
The final result of the computations is returned using
Py_Buildvalue, which takes a similar type of format string to create a Python value from your answer. We use
f here, which stands for float, to demonstrate that double and float are treated similarly: ...