Skip to content

Python Polymorphism

Polymorphism generalizes a functionality so it can work on different types. It’s an important concept in object-oriented programming.

We can define the same method on different classes:

class Dog:
    def eat():
        print('Eating dog food')

class Cat:
    def eat():
        print('Eating cat food')

Then we can generate objects and we can call the eat() method regardless of the class the object belongs to, and we’ll get different results:

animal1 = Dog()
animal2 = Cat()

animal1.eat()
animal2.eat()

We built a generalized interface and we now do not need to know that an animal is a Cat or a Dog.


→ Get my Python Handbook
→ Get my Python Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list