"str object is not callable" error

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Apr 13 06:55:18 CEST 2009


En Wed, 08 Apr 2009 18:11:37 -0300, venkat sanaka <venkatsanaka at gmail.com>  
escribió:

> i was using python/c api to call a python function from c and I know the
> name of the function which i want to call.Is there any way to do that??
> This is the method i tried...
>
> for eg:This is the python function i wants to call.
>  >>>def add(x):
> ...       return x+10
>
>
> This is my code in C:
>     PyObject *result = NULL;
>     int arg;
>     PyObject *arglist;
>     arg = 123;
>     my_callback = "add";
>     arglist = Py_BuildValue("(i)", arg);
>     result = PyObject_CallObject(my_callback, arglist);
>     Py_DECREF(arglist);
>     return result;
>
> I was getting a error like "str object is not callable".From the error i
> came to know that i was assigning "add" as a string which caused this
> error.Then how to make it a callable object??

This is what one would write in Python:

import some_module
some_module.add(123)

Do the same in C:

callback = PyObject_GetAttrString(some_module, "add");
if (!callback) ...error...
result = PyObject_CallFunction(callback, "i", arg);
Py_DECREF(callback);
return result;

-- 
Gabriel Genellina




More information about the Python-list mailing list