new-style class instance check

Richard Gruet rjgruet at yahoo.com
Sat Apr 10 20:04:41 EDT 2004


Robert

Thank you for your suggestion.
But unfortunately, isNewStyleClassInstance(o) must return False for
*everything but* an instance of a new-style class. If o is e.g. a function:
def foo(): pass
foo.__class__.rmo
<built-in method mro of type object at 0x1E0BA9E0>

so isNewStyleClassInstance(foo) would return True !

Richard

"Robert Brewer" <fumanchu at amor.org> wrote in message
news:mailman.516.1081639807.20120.python-list at python.org...
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