Join Bill Weinman for an in-depth discussion in this video Greater reusability with inheritance and polymorphism, part of Python 3 Essential Training.
The object model in Python is really very powerful and well-designed. And Python does support inheritance and polymorphism. So let's take a look at an example of how we do inheritance and polymorphism in Python. First, we'll make a working copy of oop2.py, and we'll open our working copy, and here we have a number of classes, and you'll see we have these animals: Dog, Person and Duck. And each of these inherits AnimalActions.
So AnimalActions has the code in it. It has these four methods: quack, feathers, bark and fur. And you will notice that each of these methods is defined on one line. So because each of these methods has just one line of code, you can do it all on one line. You have the def keyword, and then you have the name of the method and then the code. In this case, it's return self.strings 'quack'. This notation here is because strings is a dictionary, and we'll get into that later on in the course when we talk about data types. But these strings, self.strings, are actually defined in the classes that inherit AnimalActions, and so this is an example of inheritance.
This is also an example of data abstraction, because each of these classes has different data. So here we have the duck, and it has these four strings in it: quack, feathers, bark and fur. And the duck says quack. The duck has gray and white feathers. The duck cannot bark. The duck has no fur. And then we have a Person, which also inherits AnimalActions, and the Person has quack, feathers, bark and fur. But for quack, instead of saying quack, the person imitates a duck.
The person takes a feather from the ground and shows it for feathers. For bark, the persons says woof. For fur, the person puts on a fur coat, and then we have dog, which also has quack, feathers, bark and fur, and does dog-like things with these. So this allows us to abstract all of this data, and to use it in exactly the same way in each of these classes. Because each of these classes inherits AnimalActions, these methods become available in each of these other classes.
So when I create a duck object, here we have donald = Duck, that duck object will have these dictionary strings, Quaaaaak! The duck has gray and white feathers, and it will also have these action methods: quack, feathers, bark and fur. And so I can call duck.quack and I can call duck.feathers. The beauty of this is that I can also define a Person, john, and I can call in_the_forest, where it's expecting a duck, I can call it with john.
And so here we have an iterator. We are calling for o in ( donald, john, fido), so o will be sequentially don and john and fido. john is a person; john is not a duck. But in_the_forest(o) calls in the forest with john and in_the_forest expects a duck object, so it calls duck.quack and duck.feathers. Well, john implements - because john is a person, a person inherits AnimalActions, and it implements quack and feathers.
So this is called polymorphism. And let's see how this works. Remember, we are going to be calling in the doghouse and in the forest for each of these three objects: donald, john and fido. A Duck, a Person and a Dog. First, we'll call in the forest for each of them, and then we'll call in the doghouse for each of them. So I am going to run this. And we'll take a look at the results. In the forest, we have Quaaaak! And the duck has gray and white feathers.
So we are calling Quaaaak! and feathers for each of these. We are calling Quaaaak! and feathers also for the person, and it says the person imitates a duck, and the person takes the feather from the ground and shows it, and also for the dog, and then in the doghouse, the duck cannot bark, the duck has not fur, the person says woof! the person puts on a fur coat and then for the dog Arf! and the dog has white fur with black spots. So this is an example of polymorphism and inheritance in Python.
Now real quick, I want to also show you this example that's been rewritten as a Model-View-Controller. Model-View-Controller is a very common pattern in object-oriented programming, and I just want to show you that we can do exactly the same thing and actually separate it out a little bit further. In this case, we are not bothering to define the data for that which doesn't apply for each of these. So the dog doesn't quack, so I am not having a string for dog.
So this is the modeling, and we have the model for the duck and the person and the dog, and each of these inherits AnimalAction. And in this context, we don't really care about the implementation of AnimalActions, because all that matters is that we are modeling each of these types of objects. The view is the implementation of how do we view each of these things? So we have these four methods: bark, fur, quack and feathers. And they each call doAction with the word bark, fur, quack and feathers.
And then doAction is a local method, and you will notice it has this underscore. That doesn't make it special in Python, but it does tell the person who is reading it that this is something that's just going to be called internally. You are not going to want to call this on the object. You are just going to want to call it from inside of the object, or inside of the class. So this gets called from each of these methods. And if the action is not found in the strings, then it has a default that it does, where it says, this animal name has no action. And so that allows us to model these, and to actually expand them, and define more actions, and to not have to worry about doing a whole lot of extra modifications to the parent class. And then the controller is, of course, how we call all of this stuff, which is pretty much the same.
So this does exactly the same thing. If I go ahead and run this, we'll see that our result is exactly the same, but what we have here is a refined version of the same thing using a model, view and controller pattern. It's a contrived example, but this is commonly how we can do object-oriented programming in Python 3.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions