What kind of class does my instance belong too?

Remco Gerlich scarblac at pino.selwerd.nl
Mon Nov 13 18:25:21 CET 2000


Peter Stöhr <peter.stoehr at fh-hof.de> wrote in comp.lang.python:
> 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 create a class Spam and a class Eggs derived form
> class Spam.
> 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 :-(

if isinstance(aArg, Spam):

This is also true if it is actually an instance of Eggs
(isinstance(Instance, Class) is true when Instance is an instance
of class Class or a subclass).

R



More information about the Python-list mailing list