How to get PyTypeObject from PyObject?

C

chris

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?

Thanks
 
H

Hrvoje Niksic

chris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top