c++ call python function code example
Example 1: function in c++
#include <iostream>
using namespace std;
void function(){
cout << "I am a function!" << endl;
}
int main()
{
function();
return 0;
}
Example 2: c++ call python function
#include <Python.h>
#include <stdlib.h>
int main() {
setenv("PYTHONPATH",".",1);
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;
Py_Initialize();
pName = PyString_FromString((char*)"arbName");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, (char*)"someFunction");
if (PyCallable_Check(pFunc)) {
pValue=Py_BuildValue("(z)",(char*)"something");
PyErr_Print();
printf("Let's give this a shot!\n");
presult=PyObject_CallObject(pFunc,pValue);
PyErr_Print();
}
else
PyErr_Print();
printf("Result is %d\n",PyInt_AsLong(presult));
Py_DECREF(pValue);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return 0;
}
Example 3: double function call javascript
function add(x){
return function(y){
return x + y;
};
}
var addTwo = add(2);
addTwo(4) === 6;
add(3)(4) === 7;