Class types

Hans Nowak wurmy at earthlink.net
Tue Aug 20 12:45:11 EDT 2002


Chris Lyon wrote:

> How do I find out what type of object an instance is?
> 
> I can use isinstance but I have to know the class I am expecting, and
> if this involves sub-classes then I have long winded successions of
> isinstance where the order is very important because sub-classes are
> instances of their super class.

I think you want the __class__ attribute:

 >>> class Foo: pass

 >>> foo = Foo()
 >>> foo.__class__
<class __main__.Foo at 0x0090D808>

Or maybe, for your sorting purposes:

 >>> foo.__class__.__name__
'Foo'

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.angelfire.com/jazz/aquila/blog/blogger.html




More information about the Python-list mailing list