Major Python/C API Tools

These Python/C integration tools, available in the C library generated when Python is compiled, are used to call Python from C (embedding) and call C from Python (extending). Note that the SWIG system (among others) can automate much C extension module coding, and the Distutils tool is useful for building extensions. See the Python/C API manual for additional details and calls omitted here.


#include "Python.h"

Main Python C include file; defines the Python/C API.

libpython2.2.a, python22.dll, etc. (platform-dependent)

Python C library, when linked-in in embedded mode.


Type signature of a generic Python object in a C program.

Py_Initialize( )

Called to initialize linked-in Python libraries in embedded mode.

Py_InitModule(char *name, PyMethodDef methods_table[])

C extension module initialization call in extending mode.

Reference Counts

void Py_INCREF(PyObject *o), void Py_XINCREF(PyObject *o)

Increments the reference count for object o. Second format has no effect if o is NULL.

void Py_DECREF(PyObject *o), void Py_XDECREF(PyObject *o)

Decrements the reference count for object o. Second format has no effect if o is NULL.

Data Translation

int PyArg_ParseTuple(PyObject *arg, char *format, ...);

Converts a Python tuple to C values. arg must be a tuple (e.g., an argument list passed to a C function). format is a conversion format string, whose syntax is given in Table 1-25 and in the Python/C API manual (e.g., si specifies string and integer conversions). ...

Get Python Pocket Reference, Second 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.