Python Tutorial – Inheritance (11)

Python Tutorial – Inheritance
Python Tutorial – Inheritance

Python is a development language that has become very popular in application development. We will continue to move forward by looking at the notion of inheritance on objects in Python and how to manipulate them.

Previous tutorial:#12 – Abstract Classes

Inheritance in Classes

Continuing from our previous examples, one might think that fruits belong to a broader category of food. Therefore, we can create a class Food that will inherit from the Food class.

Food could contain parameters that are common to all foods, and Fruit could have only the parameters dedicated to the fruit while still having access to the parameters defining food more globally.

This would be written as:

class Food:
    nom = ''
class Fruit(Food):
    estFruite = ''

Here, we move the nom parameter to the Food class, called the parent class, and introduce an estFruite parameter to the child class Fruit because this parameter is not suitable for all existing foods. To create this inheritance between the two classes, we pass the parent class we want as a parameter to the child class.

This is akin to creating a model like:

  • Food -> Fruit -> banana
  • Food -> Fruit -> apple

Why not: Food -> Vegetable -> leek

We could write this program to name the object’s name, which is general to all elements:

class Food:
    nom = ''
class Fruit(Food):
    estFruite = ''
    def __init__ (self, nom, estFruite):
        self.nom = nom
self.estFruite = estFruite
banana = Fruit(‘banana’, ‘yes’)

With this concept of inheritance, it will also be possible to call functions from the parent class without any issues. So, this function will be available from all child classes.

class Food:
nom = ''
    def displayNom(self):
        print(self.nom)
class Fruit(Food):
    estFruite = ''
    def __init__ (self, nom, estFruite):
        self.nom = nom
        self.estFruite = estFruite
banana = Fruit('banana', 'yes')
banana.displayNom()

It is also possible to override the function of the parent class by defining it in the child class. If a function with the same name is in both the parent and child classes, then the function of the child class is called.

class Food:
    nom = ''
    def displayNom(self):
        print('Food')
class Fruit(Food):
    estFruite = ''
    def __init__ (self, nom, estFruite):
        self.nom = nom
        self.estFruite = estFruite
        def displayNom(self):
            print(self.nom)
banana = Fruit('banana', 'yes')
banana.displayNom()

Here, ‘banana’ will be displayed, not ‘Food’, because the displayNom function of the child class overrides that of the parent class.

Fortunately, Python has thought of everything by giving you the ability to call the function of the parent class with ease using the super() function.

Here’s a simple example:

class Food:
    nom = ''
    def displayNom(self):
        print('Food')
class Fruit(Food):
    estFruite = ''
    def __init__ (self, nom, estFruite):
        self.nom = nom
        self.estFruite = estFruite
    def displayNom(self):
        super().displayNom()
banana = Fruit('banana', 'yes')
banana.displayNom()

Multiple Inheritance

In Python, it is possible to inherit from multiple classes, not just one. We won’t go further for now, but here’s an example of our Fruit class that now inherits from Food and also from Eat.

class Food:
    nom = ''
    def displayNom(self):
        print('Food')
class Eat:
    action = 'eat'
class Fruit(Food, Eat):
    estFruite = ''
    def __init__ (self, nom, estFruite):
        self.nom = nom
        self.estFruite = estFruite
    def displayNom(self):
        super().displayNom()
banana = Fruit('banana', 'yes')
banana.displayNom()

Conclusion

We have explored new useful concepts about objects in Python, which are essential to creating quality programs. We will delve deeper into our learning journey about Python in the next chapter, which still holds many secrets.

*Next Chapter: Tutorial Python – Abstract Classes (12)

(Visited 55 times, 1 visits today)
About Judicaël Paquet 368 Articles
Judicaël Paquet (agile coach and senior devops) My Engagements in France and Switzerland: - Crafting Agile Transformation Strategies - Tailored Agile Training Programs - Raising Awareness and Coaching for Managers - Assessing Agile Maturity and Situational Analysis - Agile Coaching for Teams, Organizations, Product Owners, Scrum Masters, and Agile Coaches Areas of Expertise: Scrum, Kanban, Management 3.0, Scalability, Lean Startup, Agile Methodology.

Be the first to comment

Leave a Reply

Your email address will not be published.


*