new-style class instance check

Robert Brewer fumanchu at amor.org
Sat Apr 10 19:27:52 EDT 2004


Richard Gruet wrote:
> How to determine that an object o is for sure an instance of 
> a new-style
> class, without knowing of which specific class ?
> That is, if I define a function:
> 
> def isNewStyleClassInstance(o):
>     pass    ## to be completed
> 
> .. I want to pass the following test:
> 
> def C: pass
> def CNew(object): pass

I assume you meant "class C" instead of "def C" ;) If it's new-style,
it'll have an "mro" attribute:

>>> class OldStyle: pass
... 
>>> class NewStyle(object): pass
... 
>>> NewStyle.mro
<built-in method mro of type object at 0x00E88F38>
>>> NewStyle.mro()
[<class '__main__.NewStyle'>, <type 'object'>]
>>> OldStyle.mro()
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: class OldStyle has no attribute 'mro'


Hope that helps,

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list