O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required