new-style class instance check

Aahz aahz at pythoncraft.com
Sun Apr 11 01:20:34 EDT 2004


In article <c59uvd$fgh$1 at apollon.grec.isp.9tel.net>,
Richard Gruet <rjgruet at yahoo.com> 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
>
>assert not isNewStyleClassInstance(C()) # InstanceType
>assert isNewStyleClassInstance(CNew())
>assert not isNewStyleClassInstance(1)    # instance of type int
># and naturally for all other types of o the function should return False.

class UserClass(object): pass

class CNew(UserClass): pass

You can automate this within a module by setting __metaclass__.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?



More information about the Python-list mailing list