2

From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments.

It is not clear to me in the Python documentation how I would get a "Callable" object from the main python interpreter.

Any help appreciated in:

  1. Getting the address from the function
  2. Calling the function with my PythonObject's as arguments.

I'm using Python 2.x series for my development.

Johan Råde
21.7k22 gold badges76 silver badges112 bronze badges
asked Jul 2, 2011 at 18:57
1

1 Answer 1

5

Basically, you use the Python C API to get the module the function is contained in, then query the module dictionary for the function. That's more or less the same what the Python runtime does internally when your Python code invokes a function from somewhere.

Relevant functions from the API to look at are PyImport_ImportModule, PyModule_GetDict, PyDict_GetItem and the PyObject_CallXXX family of functions.

answered Jul 2, 2011 at 19:00
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick response. How would I get main module?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.