What kind of class does my instance belong too?

Fredrik Lundh fredrik at effbot.org
Mon Nov 13 12:28:29 EST 2000


Peter Stöhr wrote:
> 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 ?

class Eggs(Spam):
    def doSomething(self, aArg):
        if isinstance(aArg, Spam):
            self.spamIt()
        else:
            seld.doSoemthingDifferent()

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list