HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

3) Inheritance Lesson

When to Use Python Inheritance

5 min to complete · By Martin Breuss

Over the previous lessons, you got an idea of what inheritance is in OOP---that also counts for other programming languages, not just Python. You might now wonder whether all your Python code should be custom objects with child classes through inheritance.

The practical truth is that---as mentioned before---you don't have to use Python in an object-oriented manner. And even if you do, you might need to build your own inheritance relationships less often than you might expect.

If you don't need it, then it can be good to avoid inheritance to keep your program's complexity lower. It can get tough to wrap your head around inheritance relationships, especially when you dip your toes into multiple inheritance.

Unless you have a good reason for it, don't bother building your own child classes in Python. So why all the fuss about this concept?

Understand Python Inheritance

In higher-level languages such as Python, there's often a trade-off between relaxing into the abstraction of the language and understanding the nitty-gritty of how it works.

Inheritance is a core concept of OOP, and you'll benefit from having a basic understanding of it whether you apply it frequently in your code or not.

Python itself, and nearly all major third-party Python packages you'll work with, are built using inheritance. Every time you create a new class in Python, that class actually inherits from object:

class Example:
    pass

print(dir(Example()))
# OUTPUT:
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
#  '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
#  '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
#  '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Even though you defined an empty Example() class, it comes with a lot of dunder methods. These are defined in the object class that every Python class inherits from:

print(dir(object()))
# OUTPUT:
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
#  '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
#  '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
#  '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Inheritance is a fundamental concept that's part of the programming language you're working with. Therefore, understanding the concept and its syntax will help you better understand the code you're working with.

Use Inheritance

You'll also use inheritance when you work with many popular Python packages. For example, the Django web framework hides a lot of the difficulties of building a stable web application behind abstractions that allow you to build web apps faster.

For example, you can create web pages that follow a common structure with very little code by inheriting from a parent class that's defined in Django itself:

from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = "about.html"

In this example code snippet about subclassing a generic view, you import a TemplateView class from Django's built-in classes and use it as a parent class to your custom AboutView.

You don't need to understand any specifics of what a "view" is or what the context of this code snippet is. But because you understand the concept of inheritance, you can notice that you're making use of a pre-defined parent class to give your custom class a lot of attributes and functionality that you'd otherwise have to build yourself.

Summary: When to Use Python Inheritance

  • While some programs don't require inheritance, you will often work with it while using third-party Python packages
  • Inheritance can greatly reduce the amount and complexity of your code
  • Python Inheritance makes high-level code less mysterious