Python/C API: Python class instance information in C?

Bernt Ribbum bernt at
Fri Mar 21 08:59:30 CET 2003

In a C module function, is it possible to extract information about a Python
class instance being sent as argument?

To explain a bit further... A C module (METH_VARARGS wrapper) function
prototype looks something like

PyObject *spam_func(PyObject *self, PyObject *args);

The Python arguments are found (possibly inside a tuple) in "args", and I
can handle this for standard Python types like ints, floats, etc., and also
for types exported from my C module. What I stumble on, is Python code like

import spam
class X:
x = X()

In this case I don't know how to extract information about the object. All I
can find (from the C debugger) is that the object in question has an ob_type
of _PyInstance_Type. (A call to PyInstance_Check() will return true, but
unfortunately the documentation states "There are very few functions
specific to instance objects". In fact, there is only the check function
above and two New functions. Nothing else.)

Is it possible for me to get more information here? I would like to know, at
least, that this object is of type "class X" or similar.

Any help appreciated :-)

- Bernt
Storgata 20  0184 OSLO  NORWAY
Tel +47 2315 8700  Fax +47 2315 8701
Bernt Ribbum  bernt at  bernt.ribbum at
Direct: +47 2315 8714  Mobile: +47 906 106 85

More information about the Python-list mailing list