Python language basics 79: polymorphism basics
February 14, 2016 Leave a comment
In the previous post we discussed composite classes in Python. Composite classes are classes that are at least partly made up of other classes. We saw that there wasn’t anything special about adding other classes as class level properties to a class. In our example we constructed a Name, an Address and a Telephone classes that were all parts of the Person class.
In this post we’ll start looking into the key object-oriented idea of polymorphism. This is a relatively large topic and will span a couple of posts. In this post we’ll look at some theory first to pave the way for the code examples in upcoming posts.
Polymorphism in related objects
If you are new to programming and object-oriented design then this word may sound scary at first. You may think of some overly complex language feature. However, it is not that mysterious at all. Here’s a definition of polymorphism on Wikipedia:
“In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types. A polymorphic type is one whose operations can also be applied to values of some other type, or types.”
Doesn’t make any sense to you? Polymorphism comes in different shapes in programming languages. Let’s say that you’d like to model animals in your code: cats, dogs, and birds. Most of those animals will be able to produce some kind of a sound. Dogs bark, cats meow, birds tweet and so on. This sound making ability is a common “feature” for those animals. In code you could have different methods that represent the sound making “logic”: bark(), meow() and tweet() in the classes Dog, Cat and Bird.
However, as these animals can all produce a sound it’s wise to show it in code as well in some common function called make_sound(). All animals will then implement make_sound in their own ways:
def make_sound(self): print("Wooff") def make_sound(self): print("Meeoooww") def make_sound(self): print("Chirp-chirp)
That’s an example of polymorphism. The modeled animal types can all make a sound but in their own special ways. The caller of the Cat, Bird and Animal classes shouldn’t be aware of the exact implementation details of make_sound(). The only thing that matters is that the objects will produce some sound when calling make_sound().
An important characteristic of this type of polymorphism is that the implementing objects are all related types: they are all… …animals, right? We’ll come back later to this type of polymorphism and learn about things like base classes and derived classes.
Polymorphism in unrelated objects
Polymorphism can also occur in unrelated classes. A classic example is comparison. We can compare cars with cars and guitars with guitars but not necessarily cars with guitars. However, both the Guitar and Car class will have a “compare” method bringing them to the same platform: they are both comparable objects. However, the two objects are otherwise completely unrelated. I tend to call this type of polymorphism interface polymorphism as they both implement an interface. Interfaces are commonplace in strongly typed languages such as C#, Java, VB .NET etc.
We’ll see an example of interface polymorphism in the next post.
Read all Python-related posts on this blog here.