How does polymorphism work in Python?

Posted by froadie on Stack Overflow See other posts from Stack Overflow or by froadie
Published on 2010-05-14T16:23:42Z Indexed on 2010/05/14 16:34 UTC
Read the original article Hit count: 304

Filed under:
|

I'm new to Python... and coming from a mostly Java background, if that accounts for anything.

I'm trying to understand polymorphism in Python. Maybe the problem is that I'm expecting the concepts I already know to project into Python. But I put together the following test code:

class animal(object):
    "empty animal class"

class dog(animal):
    "empty dog class"

myDog = dog()
print myDog.__class__ is animal
print myDog.__class__ is dog

From the polymorphism I'm used to (e.g. java's instanceof), I would expect both of these statements to print true, as an instance of dog is an animal and also is a dog. But my output is:

False
True

What am I missing?

© Stack Overflow or respective owner

Related posts about python

Related posts about polymorphism