Introduction
Inheritance is an advanced Python concept that you probably won’t use when starting out. Most beginner programs work perfectly fine without it. However, as your projects grow, inheritance can make your code much cleaner by avoiding repetition.Don’t worry if inheritance feels complex at first. Focus on understanding basic classes, and come back to inheritance when you find yourself writing similar classes with shared functionality.
What is inheritance?
Inheritance lets you create new classes based on existing ones. The new class (child) gets everything from the parent class, plus can add its own stuff. Think of it like this:- All dogs are animals (dogs inherit from animals)
- Dogs have everything animals have, plus dog-specific things
Basic inheritance example
Adding attributes in child classes
Child classes can have their own attributes too:super().__init__() calls the parent class’s __init__ method. This ensures the parent class sets up its attributes properly before the child class adds its own.Overriding methods
Child classes can change how parent methods work:Real-world use case
Here’s a practical example for AI applications:Types of Inheritance in Python
Python is extremely versatile and supports five primary types of inheritance:- Single Inheritance: A child class inherits from a single parent class. (e.g.,
class Dog(Animal)) - Multiple Inheritance: A child class inherits from more than one parent class. (e.g.,
class SmartDevice(Camera, Phone)) - Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits from another class. (e.g.,
class Puppy(Dog)whereDoginherits fromAnimal) - Hierarchical Inheritance: Multiple child classes inherit from a single parent class. (e.g., both
DogandCatinherit fromAnimal) - Hybrid Inheritance: A combination of two or more of the above inheritance types.
Conflict Resolution & Method Resolution Order (MRO)
In Multiple Inheritance, a conflict arises if two parent classes define the same method or attribute. How does Python decide which one to execute?The MRO Resolution Rules:
Python resolves conflicts using the Method Resolution Order (MRO), which follows the C3 Linearization algorithm. The order is determined by:- Looking at the class itself first.
- Looking at parent classes from left to right as specified in the class definition (e.g.,
class Child(ParentA, ParentB)looks atParentAbeforeParentB). - Moving up the hierarchy without visiting the same class twice (depth-first, left-to-right, but keeping shared base classes for last).
Visualizing MRO:
You can print the MRO of any class using the.mro() method or the __mro__ attribute.
class Child(ParentB, ParentA), then c.greet() will output "Hello from Parent B".
When to use inheritance
Use inheritance when:- You have an “is a” relationship (Dog is an Animal)
- Child classes share most behavior with parent
- You want to extend functionality, not replace it
- Classes are only slightly related
- You just want to reuse one or two methods
- The relationship feels forced
Common mistakes
Forgetting to call super().__init__()
Forgetting to call super().__init__()
Wrong inheritance relationship
Wrong inheritance relationship
What’s next?
Let’s explore when to use classes and when to keep things simple.When to use classes
Best practices and guidelines