Last modified: Dec 01, 2026
Inheritance in Python Explained
Inheritance is one of the core ideas in object-oriented programming. It lets a class reuse code from another class. This keeps your programs short and easy to maintain.
In Python, inheritance is simple to set up. You write the parent class name in parentheses after the child class name. That is all it takes to start.
What Is Inheritance?
Inheritance is a way to build a new class from an existing one. The existing class is called the parent or base class. The new class is called the child or derived class.
The child class gets all the methods and attributes of the parent. You can then add new features or change old ones. This saves you from writing the same code twice.
If you are new to classes, start with the basics in Python class basics for beginners. It will make inheritance much easier to follow.
A Simple Example
Let us look at a small example. We create a parent class called Animal. Then we create a child class called Dog.
class Animal:
def __init__(self, name):
self.name = name # store the animal name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal): # Dog inherits from Animal
def speak(self):
return f"{self.name} barks."
dog = Dog("Rex")
print(dog.speak())
Rex barks.
Notice that Dog did not define __init__. It got that method from Animal for free. The child only changed the speak method.
Why Use Inheritance?
Inheritance has several clear benefits. It reduces repeated code. It makes your program easier to read. It also makes changes faster because you edit code in one place.
Imagine ten classes that all need a save method. Without inheritance, you write it ten times. With inheritance, you write it once in a parent class.
You can also build a clean class structure. This helps when your project grows. For a deeper walkthrough, see Python inheritance made simple.
Types of Inheritance in Python
Python supports several forms of inheritance. Each one has its own use case.
Single inheritance means one child class and one parent class. This is the most common form.
Multiple inheritance means one child class with more than one parent. Python allows this, but use it with care.
Multilevel inheritance means a chain, such as A to B to C. The child at the end gets features from all levels above it.
Hierarchical inheritance means many child classes share one parent class.
Using super() to Call the Parent
Sometimes the child needs its own __init__ method. When that happens, you should still run the parent setup. The super() function does this for you.
class Animal:
def __init__(self, name):
self.name = name # parent sets the name
def speak(self):
return f"{self.name} makes a sound."
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name) # call the parent __init__
self.color = color # child adds a new attribute
def speak(self):
return f"{self.name} meows."
cat = Cat("Milo", "black")
print(cat.speak())
print(cat.color)
Milo meows.
black
Using super() keeps your code safe. If the parent class changes later, the child still works. It also avoids calling the parent class name directly.
The __init__ method is a special method. You can learn more about these in the Python dunder methods list.
Overriding Methods
Overriding means the child class replaces a parent method. You saw this in the examples above. The child speak method replaces the parent one.
You do not need any special keyword for this. Just define a method with the same name in the child class. Python will use the child version.
This gives you flexibility. You can keep a shared design and still let each child behave in its own way.
Multiple Inheritance and the MRO
Python allows a class to inherit from more than one parent. When methods have the same name, Python needs a rule to pick one. That rule is called the Method Resolution Order, or MRO.
The MRO follows a left-to-right order. Python checks the first parent, then the next, and so on. You can view it with the mro method.
class A:
def hello(self):
return "Hello from A"
class B:
def hello(self):
return "Hello from B"
class C(A, B): # A comes first
pass
obj = C()
print(obj.hello())
print(C.mro())
Hello from A
[, , , ]
The output shows that A comes before B. So the hello method from A is used.
Special methods like __str__ are also affected by inheritance. Read more in the Python magic methods complete list.
Inheritance and Properties
Child classes also inherit properties. That means getters and setters defined in the parent work in the child too.
You can add new properties in the child class as well. This keeps your data access clean and controlled.
If properties are new to you, check out this guide on Python property vs attribute. It explains the difference in plain terms.
When to Avoid Inheritance
Inheritance is powerful, but it is not always the right tool. Deep class chains can become hard to follow. Small changes in a parent can break many children.
Sometimes a simple class with arguments works better. You can also use composition, where one class holds another as an attribute.
A good rule is to use inheritance for a clear "is-a" relationship. A dog is an animal. A car is a vehicle. If the relationship is weak, think twice.
Common Mistakes
One common mistake is forgetting to call super().__init__(). The parent attributes then stay unset, and your code fails later.
Another mistake is using multiple inheritance without a plan. The MRO can surprise you. Keep it simple and document your class layout.
A third mistake is copying code instead of inheriting it. If two classes share logic, move that logic to a parent class.
Conclusion
Inheritance in Python lets one class reuse the code of another. You write the parent class once. The child class gets its methods and attributes for free.
Use super() to run parent setup. Override methods when the child needs its own behavior. Watch the MRO when you use multiple parents.
Start with small examples like the ones above. Then try it in your own projects. With practice, inheritance becomes a natural part of your Python toolkit.