How to get PyTypeObject from PyObject?

Hrvoje Niksic hniksic at xemacs.org
Mon Aug 25 17:19:31 EDT 2008


chris <cgrebeld at gmail.com> writes:

> I want to check the type of an object, right now I am doing
> strcmp( object->ob_type->tp_name, "MyClass")
>
> Which seems slow, is there a way I can find the pointer to the
> global PyTypeObject for MyClass, and then just call
> PyObject_TypeCheck?

Simply get the PyObject that corresponds to MyClass using
PyObject_GetAttr on the module (or in some other way).  If MyClass is
a new-style class, you can cast it to PyTypeObject and compare that to
object->ob_type.

For questions about the C API, see the capi-sig,
http://mail.python.org/mailman/listinfo/capi-sig



More information about the Python-list mailing list