new-style class instance check (bis)

Richard Gruet rjgruet at yahoo.com
Sat Apr 10 20:05:19 EDT 2004


(just a reformulation of my previous post)

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:

class OldStyle: pass
class NewStyle(object): pass

assert not isNewStyleClassInstance(OldStyle()) # InstanceType
assert isNewStyleClassInstance(NewStyle())
assert not isNewStyleClassInstance('hi')    # "instance" of type str

# and naturally for all other types of o the function should also return
False, e.g.

def foo(): pass
assert not isNewStyleClassInstance(foo) # FunctionType
assert not isNewStyleClassInstance(OldStyle) # ClassType
# etc...


Richard






More information about the Python-list mailing list