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?
isinstance(myDog, animal)
does what you're looking for,myDog.__class__ is animal
is wrong. Also in Python we use MixedCase for class names but lower_case_with_underscores for object names. So your classes should be calledAnimal, Dog
and your objectmy_dog, dog1
etc. – Drummond