From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Class inheritance: Example

Class inheritance: Example

- [Instructor] A fun way to explore the concept of class inheritance in Python is to use Python turtle graphics again. I'm just going to run the code on my screen and then I'm going to come back and explain how it works. So we have a turtle and when we click on it, it changes color. Now I've done this using class inheritance. On line 24, my_turtle is an instance of a special type of turtle called RainbowTurtle. It has all the characteristics of a normal turtle but with a few changes. So instead of the default shape for a new turtle, for example, we've made sure that instances of RainbowTurtle are turtle shaped as soon as they're created. This is done on line eight. A lot of the magic here is down to the line super__init. That is on line seven. What this does is it calls the constructor of the parent class, so all of its properties and methods are available to the child object being created. As you can see on line five, the name of the parent class goes in parenthesis after your class…

Contents