Classic and New Style Classes?

Michele Simionato michele.simionato at gmail.com
Thu Jun 24 12:21:52 CEST 2004


"Chris S." <chrisks at NOSPAMudel.edu> wrote in message news:<cbdslo$62k$1 at scrotar.nss.udel.edu>...
> I'm generating the source of an object from a list of objects. Therefore 
> I need to determine if the object is a class definition (i.e. 'def 
> something(whatever):') or a class instance (i.e. 'somename = 
> somethingelse()'). With classic classes this is trivial, since all I 
> have to do is check the type. However, for the new "improved" style 
> classes this seems next to impossible, since everything is essentially 
> an instance of something.
> 
> isinstance(class, classinfo) won't work since I won't necessarily know 
> classinfo.
> 
> Is there any way to make this distinction? Any help is appreciated.

>>> from types import ClassType
>>> class Old: pass
...
>>> isinstance(Old, ClassType)
True
>>> class New(object): pass
...
>>> isinstance(New, type)
True

Also Old.__class__ raises an error whereas New.__class__ returns type(New).

Is this what you are asking for?

     Michele Simionato



More information about the Python-list mailing list