Python language basics 79: polymorphism basics

Introduction

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: