This is a follow up from the prior Python post on Classes.
Let’s say you have already built 1 class. Let’s say you are now tasked with building 10 more classes which are similar to the 1st class that you built. Instead of repeating and copying the same code over and over again. It would be much better to have these new clones INHERIT the parts that you need from your original class.
Table of Contents:
Object Oriented Programming
Parent Class/Base Class
Child Class/Derived Class
Exercises
1 - Object Oriented Programming
From this post (Python Classes), we had a introduction on how to create classes, and what the object class is. We also touched on what Object Oriented Programming (OOP) means. Also, why Python is considered an object oriented programming language.
Python allows objects to be created from user-defined types (classes). This allows for custom objects to be implemented. If you cover topics related to game programming, then you can create a game library class. In this class you'll have attributes such as the game board size and quantity of tokens per player.
Python also supports multiple inheritances – that is, classes being created from more than one parent class. This allows programmers to define complex systems with fewer lines of code.
2 - Parent Class/Base Class
If we want to create two classes in Python. But, we want the second one to be based off the first one. Then, this would be an example of class inheritance. Where the second class inherits the properties from the first class.
The original is called the Parent class/Base Class. The copy is called the child class/derived class.
To create our original Parent class. We create this class as we would. So, to keep things simple, we can use our old example from the Python Classes post.
class BowTied:
Twitter=True
favorite_number=69
def __init__(self,substack,maturity):
self.substack=substack
self.maturity=maturity
Raptor=BowTied(substack='bowtiedraptor.substack',maturity='zoomer')
Here is some example of the class instances in action.
3 - Child Class/Derived Class
3.1 Inherit All The Methods
As stated above, the parent class is the original class. The child class are the copies. For this example, we'll make a class called myclass. We'll also have this class inherit the attributes and methods from the existing class.
We make a class as normal, but, after the class name, we use parenthesis, and point to our parent class in the brackets. In other words:
class BowTied:
Twitter=True
favorite_number=69
def __init__(self,substack,maturity):
self.substack=substack
self.maturity=maturity
class myclass(BowTied):
pass
In the example above, after a colon. If you do not wish to add in any more details to the function body, you can use the pass keyword to leave it there. Here is an example of me inhering all the methods, and then doing a simple example.
3.2 Additional Attributes
Going further with my class called myclass. What if I wanted to add further attributes/methods to it. In this case, I can still use the def __init__ self argument to add in more fields to my class. In this example, I'll add an extra instance attribute called emoji. In this attribute I'll add in a simple emoji for this child class. Unfortunately, I cannot use the def __init__ function by itself. Doing this will cause the child class to overwrite the attributes of the parent class. The following code below demonstrates this error:
Wrong Way:
To fix this, I'll want to inherit everything from the parent class using the super() function. Then load up an extra attribute using the def __init__ self. Example code below:
Correct Way:
3.3 Single Parent Class vs Multiple Parent Classes
If we are making a child class, sometimes it makes sense to build this from many super class. To save up space, I'll make 3 generic parent classes, and then 1 basic child class. To work with many parent classes, use a comma to separate each of the parent classes, when you create your child class.
Here is some example code below:
class generic1:
x=1
class generic2:
y=2
class generic3:
z=3
class basic(generic1,generic2,generic3):
pass
my_basic=basic()
Here is an example, with some example output:
We can use the exact same idea as above and use the def __init__ self argument in order to give it several instance attributes as well.
4 - Exercises
Here’s some Python exercises you can try:
Given the code for class vroom, make another class called broom that inherits everything from vroom.
class vroom: def __init__(self, name, crash, global_warming): self.name = name self.crash = crash self.global_warming = global_warming
Now adjust the code generated from 1. and give another attribute the broom class called sweep, and assign this to be True. Note, sweep would be a class attribute, not an instance attribute.
Click Here, and do some more additional practice questions.
class vroom:
def __init__ (self, name, crash, global_warming):
self.name = name
self.crash = crash
self.global_warming = global_warming
class broom(vroom):
def __init__ (self, name, crash, global_warming, sweep):
super().__init__(name, crash, global_warming)
self.sweep = sweep
abc = broom(name = 'Raptor', crash = 'Yes', global_warming = 'Fake News lol', sweep = True)
print(
'name = ', abc.name,', ',
'crash = ', abc.crash,', '
'global_warming = ', abc.global_warming, ', ',
'sweep = ', abc.sweep
)
Very nice link to more exercises 🤓