473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

access to python function from C code

Vin
Is it possible to access a function defined in python shell from c-code?

For example if I have defined
def c(x): .... return x*x
....

and defined in some module foo

static PyObject *
ptk_doit(PyObject *self, PyObject *args)
{

get reference to a function here from python interpreter and apply the
function to a given value ...

return Py_BuildValue("d",result);
}
then I can do ...
import foo
foo.doit(c,4)

16

Stuff like this can be done in C when one just transfers the pointer to a
function through input arguments
but then the transfered functions are always set at the time of compilation.
It would be useful if the above can be done, then any function can be
supplied to an underlying C code
without compilation. Probably the performance would be affected but that my
not be a problem in some cases.

Needs like this arise in Optimization for example when
objective function can be arbitrary and optimization is done with general
optimization routines written in C.

Vin
Jul 18 '05 #1
1 1482
Vin wrote:
Is it possible to access a function defined in python shell from c-code?

For example if I have defined
def c(x): ... return x*x
...

and defined in some module foo

static PyObject *
ptk_doit(PyObject *self, PyObject *args)
{

get reference to a function here from python interpreter and apply the
function to a given value ...
Yes, it can be done. Exactly how depends on how you want to call this
C-coded function, see below.

return Py_BuildValue("d",result);
}
then I can do ...
import foo
foo.doit(c,4)
16


So you want to pass the function object and its single argument? OK, BUT:
function can be supplied to an underlying C code
without compilation. Probably the performance would be affected but that
my not be a problem in some cases.

Needs like this arise in Optimization for example when
objective function can be arbitrary and optimization is done with general
optimization routines written in C.


....that's very unlikely to be a sensible tack because you'll call that
function so MANY times the (big) performance impact will hurt.

Still, whatever makes you happy (warning, untested code)...:

PyObject *func, *arg, *resultobj, *floatresult;
double result;

if(!PyArg_ParseTuple(args, "OO", &func, &arg))
return NULL;
resultobj = PyObject_CallFunctionObjArgs(func, arg, NULL);
if (!resultobj)
return NULL;
floatresult = PyNumber_Float(resultobj);
Py_DECREF(resultobj);
if (!floatresult)
return NULL;
result = PyFloat_AS_DOUBLE(floatresult);
Py_DECREF(floatresult);

this should be an acceptable body for ptk_doit before its return
statement.
Alex

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Uggen Kristin T | last post by:
For the first time, I am trying to program python in a windows-environment, and now I am having some trouble with combining python and MS Access... This is what I want to do: A MS Access...
7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.