Re: "str object is not callable" error

Discussion in 'Python' started by Gabriel Genellina, Apr 13, 2009.

  1. En Wed, 08 Apr 2009 18:11:37 -0300, venkat sanaka <>
    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
     
    Gabriel Genellina, Apr 13, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,688
  2. JavaQueries
    Replies:
    1
    Views:
    3,695
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,129
  4. Randall Parker
    Replies:
    5
    Views:
    55,945
    James Stroud
    Mar 22, 2006
  5. Bishop
    Replies:
    1
    Views:
    800
    Bishop
    Feb 24, 2007
Loading...

Share This Page