Calling Python from C

Sometimes it’s useful to call Python functions from C programs. To do this, the following functions can be used:

						PyObject *PyEval_CallObject(PyObject
						*func,
						PyObject
						*args)

Calls func with arguments args. func is a Python callable object (function, method, class, and so on). args is a tuple of arguments.

						PyObject *PyEval_CallObjectWithKeywords(PyObject
						*func,
						PyObject
						*args,
						PyObject
						*kwargs)

Calls func with positional arguments args and keyword arguments kwargs. func is a callable object, args is a tuple, and kwargs is a dictionary.

The following example illustrates the use of these functions:

 /* Call a python function */ PyObject *func; /* Callable object. */ PyObject *args; PyObject *result; int arg1, arg2; func = ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.