473,512 Members | 15,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1487
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
1779
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
2159
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
8481
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
3537
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
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.