Single test for a class and all its subclasses?

Anthra Norell anthra.norell at tiscalinet.ch
Thu Jun 16 15:45:41 EDT 2005


----- Original Message ----- 
From: "Konstantin Veretennicov" <kveretennicov at gmail.com>
To: "Anthra Norell" <anthra.norell at tiscalinet.ch>
Cc: "Python SIG" <python-list at python.org>
Sent: Wednesday, June 15, 2005 11:28 AM
Subject: Re: Single test for a class and all its subclasses?


> On 6/15/05, Anthra Norell <anthra.norell at tiscalinet.ch> wrote:
> >  
> > class C: ... 
> > class C2 (C): ... 
> >  
> > # What I want to do: 
> >  
> > if x.__class__ in (C, C2): 
> >    do_something_with (x) 
> 
> If you have an instance, you can use isinstance() built-in.
> 

Exactly what I've been looking for:

isinstance (c, C)     -> True
isinstance (c, C2)   -> False
isinstance (c2, C2) -> True
isinstance (c2, C)   -> True

Thank you very much!

Frederic






More information about the Python-list mailing list