What kind of class does my instance belong too?

Neil Cerutti cerutti at together.net
Mon Nov 13 13:53:28 EST 2000


Peter Stöhr posted:
>Hi out there in the python-universe,
>
>I'm having a small problem and after looking at the docs I was
>not able to solve it by myself. So maybe someone out there can
>help me.
>
>Lets assume I have created a class Spam and a class Eggs derived
>from class Spam. 

That's one messed-up universe you'd be modeling. ;-)

>In one of the methods I have to implement a behaviours depending
>on whether a passed instance is an instance of class Spam or
>something different, for example:
>
>class Spam:
>    ...
>
>class Eggs(Spam):
>    def doSomething(self, aArg):
>        if aArg is instance of Spam:
>            self.spamIt()
>        else:
>            seld.doSoemthingDifferent()
>
>What I try to figure out is what is the correct way to write such an
>if-statement ?
>
>I've tried the builtin function type(). The result <type 'instance'> is
>correct bit doesn't solve my problem :-(

You were amazingly close to the solution in your guess above. Use
the isinstance built-in function.
 
   if isinstance(aArg, Spam):

-- 
Neil Cerutti <cerutti at together.net>
Linux on board. It is now safe to turn on your computer.



More information about the Python-list mailing list