how to find out if an object is a class?
pavlovevidence at gmail.com
Fri Aug 8 03:26:46 CEST 2008
On Aug 7, 8:56 pm, Terry Reedy <tjre... at udel.edu> wrote:
> Wojtek Walczak wrote:
> > Dnia Thu, 7 Aug 2008 14:36:37 -0700 (PDT), szczepiq napisa (a):
> >> Pardon me for most likely a dummy question but how do I find out if an
> >> object is a class?
> > Use types.ClassType:
> >>>> class Q:
> > ... pass
> > ...
> >>>> import types
> >>>> isinstance(Q, types.ClassType)
> >>>> True
> That is only true and only works for 2.x old-style classes and not for
> 2.x new-style classes and all 3.0 classes, for which isinstance(Q,type)
> is True.
isinstance(Q,type) is also true for built in types and C extension
types, which may or may not be what the OP wants.
The most accurate way I can think of to check for a class defined in
Python is to test the type's tp_flags field for Py_TPFLAGS_HEAPTYPE
bit, but I don't know of any simple way to check for it from Python.
It's still possible for it to fail since someone could create heap
types in C though I'd expect that's very rare.
More information about the Python-list