1.2 Objects, Types and Reference Counts

Most Python/C API functions have one or more arguments as well as a return value of type PyObject *. This type is a pointer to an opaque data type representing an arbitrary Python object. Since all Python object types are treated the same way by the Python language in most situations (e.g., assignments, scope rules, and argument passing), it is only fitting that they should be represented by a single C type. All Python objects live on the heap: you never declare an automatic or static variable of type PyObject, only pointer variables of type PyObject * can be declared.

All Python objects (even Python integers) have a type and a reference count. An object's type determines what kind of object it is (e.g., an integer, a list, or a user-defined function; there are many more as explained in the Python Reference Manual). For each of the well-known types there is a macro to check whether an object is of that type; for instance, "PyList_Check(a)" is true iff the object pointed to by a is a Python list.


guido@python.org