Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025

      I may have found the ultimate monitor for conferencing and productivity, but it has a few weaknesses

      June 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      May report 2025

      June 2, 2025
      Recent

      May report 2025

      June 2, 2025

      Write more reliable JavaScript with optional chaining

      June 2, 2025

      Deploying a Scalable Next.js App on Vercel – A Step-by-Step Guide

      June 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025
      Recent

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»What is Polymorphism in Python? Explained with an Example

    What is Polymorphism in Python? Explained with an Example

    February 6, 2025

    Polymorphism is an object-oriented programming (OOP) principle that helps you write high quality, flexible, maintainable, reusable, testable, and readable software. If you plan to work with object-oriented software, it is crucial to understand polymorphism.

    What is Polymorphism?

    The word polymorphism is derived from Greek, and means “having multiple forms”:

    • Poly = many

    • Morph = forms

    In programming, polymorphism is the ability of an object to take many forms.

    The key advantage of polymorphism is that it allows us to write more generic and reusable code. Instead of writing separate logic for different classes, we define common behaviours in a parent class and let child classes override them as needed. This eliminates the need for excessive if-else checks, making the code more maintainable and extensible.

    MVC frameworks like Django use polymorphism to make code more flexible. For example, Django supports different databases like SQLite, MySQL, and PostgreSQL. Normally, each database requires different code to interact with it, but Django provides a single database API that works with all of them. This means you can write the same code for database operations, no matter which database you use. So, if you start a project with SQLite and later switch to PostgreSQL, you won’t need to rewrite much of your code, thanks to polymorphism.

    In this article, to make things easy to understand, I’ll show you a bad code example with no polymorphism. We’ll discuss the issues that this bad code causes, and then solve the issues by refactoring the code to use polymorphism.

    (Btw, if you learn better by video, checkout my Polymorphism in Python YouTube video.)

    First, an example with no polymorphism:

    class Car:
        def __init__(self, brand, model, year, number_of_doors):
            self.brand = brand
            self.model = model
            self.year = year
            self.number_of_doors = number_of_doors
    
        def start(self):
            print("Car is starting.")
    
        def stop(self):
            print("Car is stopping.")
    
    class Motorcycle:
        def __init__(self, brand, model, year):
            self.brand = brand
            self.model = model
            self.year = year
    
        def start_bike(self):
            print("Motorcycle is starting.")
    
        def stop_bike(self):
            print("Motorcycle is stopping.")
    

    Let’s say that we want to create a list of vehicles, then loop through it and perform an inspection on each vehicle:

    # Create list of vehicles to inspect
    vehicles = [
        Car("Ford", "Focus", 2008, 5),
        Motorcycle("Honda", "Scoopy", 2018),
    ]
    
    # Loop through list of vehicles and inspect them
    for vehicle in vehicles:
        if isinstance(vehicle, Car):
            print(f"Inspecting {vehicle.brand} {vehicle.model} ({type(vehicle).__name__})")
            vehicle.start()
            vehicle.stop()
        elif isinstance(vehicle, Motorcycle):
            print(f"Inspecting {vehicle.brand} {vehicle.model} ({type(vehicle).__name__})")
            vehicle.start_bike()
            vehicle.stop_bike()
        else:
            raise Exception("Object is not a valid vehicle")
    

    Notice the ugly code inside the for loop! Because vehicles is a list of any type of object, we have to figure out what type of object we are dealing with inside each loop before we can access any information on the object.

    This code will continue to get uglier as we add more vehicle types. For example, if we extended our codebase to include a new Plane class, then we’d need to modify (and potentially break) existing code – we’d have to add another conditional check in the for loop for planes.

    Introducing: Polymorphism…

    Cars and motorcycles are both vehicles. They both share some common properties and methods. So, let’s create a parent class that contains these shared properties and methods:

    Parent class (or “superclass”):

    class Vehicle:
        def __init__(self, brand, model, year):
            self.brand = brand
            self.model = model
            self.year = year
    
        def start(self):
            print("Vehicle is starting.")
    
        def stop(self):
            print("Vehicle is stopping.")
    

    Car and Motorcycle can now inherit from Vehicle. Let’s create the child classes (or “subclasses”) of the Vehicle superclass:

    class Car(Vehicle):
        def __init__(self, brand, model, year, number_of_doors):
            super().__init__(brand, model, year)
            self.number_of_doors = number_of_doors
    
        # Below, we "override" the start and stop methods, inherited from Vehicle, to provide car-specific behaviour
    
        def start(self):
            print("Car is starting.")
    
        def stop(self):
            print("Car is stopping.")
    
    class Motorcycle(Vehicle):
        def __init__(self, brand, model, year):
            super().__init__(brand, model, year)
    
        # Below, we "override" the start and stop methods, inherited from Vehicle, to provide bike-specific behaviour
    
        def start(self):
            print("Motorcycle is starting.")
    
        def stop(self):
            print("Motorcycle is stopping.")
    

    Car and Motorcycle both extend Vehicle, as they are vehicles. But what’s the point in Car and Motorcycle both extending Vehicle if they are going to implement their own versions of the start() and stop() methods? Look at the code below:

    # Create list of vehicles to inspect
    vehicles = [Car("Ford", "Focus", 2008, 5), Motorcycle("Honda", "Scoopy", 2018)]
    
    # Loop through list of vehicles and inspect them
    for vehicle in vehicles:
        if isinstance(vehicle, Vehicle):
            print(f"Inspecting {vehicle.brand} {vehicle.model} ({type(vehicle).__name__})")
            vehicle.start()
            vehicle.stop()
        else:
            raise Exception("Object is not a valid vehicle")
    

    In this example:

    • We have a list, vehicles, containing instances of both Car and Motorcycle.

    • We iterate through each vehicle in the list and perform a general inspection on each one.

    • The inspection process involves starting the vehicle, checking its brand and model, and stopping it afterwards.

    • Despite the vehicles being of different types, polymorphism allows us to treat them all as instances of the base Vehicle class. The specific implementations of the start() and stop() methods for each vehicle type are invoked dynamically at runtime, based on the actual type of each vehicle.

    Because the list can only contain objects that extend the Vehicle class, we know that every object will share some common fields and methods. This means that we can safely call them, without having to worry about whether each specific vehicle has these fields or methods.

    This demonstrates how polymorphism enables code to be written in a more generic and flexible manner, allowing for easy extension and maintenance as new types of vehicles are added to the system.

    For example, if we wanted to add another vehicle to the list, we don’t have to modify the code used to inspect vehicles (“the client code”). Instead, we can just extend our code base (that is, create a new class), without modifying existing code:

    class Plane(Vehicle):
        def __init__(self, brand, model, year, number_of_doors):
            super().__init__(brand, model, year)
            self.number_of_doors = number_of_doors
    
        def start(self):
            print("Plane is starting.")
    
        def stop(self):
            print("Plane is stopping.")
    
    # Create list of vehicles to inspect
    vehicles = [
        Car("Ford", "Focus", 2008, 5),
        Motorcycle("Honda", "Scoopy", 2018),
    
        ########## ADD A PLANE TO THE LIST: #########
    
        Plane("Boeing", "747", 2015, 16),
    
        ############################################
    ]
    

    The code to perform the vehicle inspections doesn’t have to change to account for a plane. Everything still works, without having to modify our inspection logic.

    Conclusion

    Polymorphism allows clients to treat different types of objects in the same way. This greatly improves the flexibility of software and maintainability of software, as new classes can be created without you having to modify (often by adding extra if/else if blocks) existing working and tested code.

    Further Learning

    Polymorphism is related to many other object-oriented programming principles, such as dependency injection and the open-closed SOLID principle. If you’d like to master OOP, then check out my Udemy course:

    • Python OOP: Object Oriented Programming From Beginner to Pro 🎥

    If you prefer book to video, check out my books:

    • Amazon Kindle and paperback 📖

    • Gumroad PDF 📖

    Thanks for reading 🙂

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleAI Engineering Roadmap
    Next Article Build a Full Stack App with Next.js and Strapi

    Related Posts

    Security

    ⚡ Weekly Recap: APT Intrusions, AI Malware, Zero-Click Exploits, Browser Hijacks and More

    June 2, 2025
    Security

    Qualcomm fixes three Adreno GPU zero-days exploited in attacks

    June 2, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Elden Ring, Dark Souls fans raise over $30,000 for beloved community member’s medical bills in one afternoon: “This has already smashed every expectation”

    News & Updates

    Smashing Security podcast #399: Honey in hot water, and reset your devices

    Development

    Taxi From Maidstone to Heathrow Airport

    Web Development

    Create Preview Deployments on Forge with Laravel Harbor

    Development

    Highlights

    Metro Bundler 3.0: Supercharged Builds & a Smoother Developer Workflow

    April 21, 2025

    Post Content Source: Read More 

    Cut Your Losses in Large-Vocabulary Language Models

    February 7, 2025

    This AI Research from Ohio State University and CMU Discusses Implicit Reasoning in Transformers And Achieving Generalization Through Grokking

    July 9, 2024

    Risoluzione del conflitto tra OBS Studio e Fedora per il pacchetto Flatpak

    February 24, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.