Mastering Dependency Inversion in Python with Abstract Base Classes

ArjanCodes////3 min read

Overview of Dependency Inversion

(the 'D' in ) transforms how components interact. Instead of high-level modules depending directly on low-level implementation details, both rely on abstractions. This decoupling ensures that changing a specific tool or library doesn't break the core logic of your application. It鈥檚 the difference between a house wired directly to one specific brand of lightbulb and a house using a standard socket that accepts any bulb.

Prerequisites

To follow this tutorial, you should understand class syntax and basic inheritance. Familiarity with (OOP) concepts like methods and constructors will help you grasp how objects communicate.

Key Libraries & Tools

  • (Abstract Base Classes): A built-in Python module used to define blueprints for other classes. It ensures subclasses implement specific methods.
  • : A static type checker that helps developers catch type-related errors before the code runs.

Code Walkthrough

1. Defining the Abstraction

First, we create an interface using the abc module. This defines what it means to be "switchable" without worrying about the device type.

from abc import ABC, abstractmethod

class Switchable(ABC):
    @abstractmethod
    def turn_on(self):
        pass

    @abstractmethod
    def turn_off(self):
        pass

2. Implementing the Subclasses

Now, we create a and a . Both inherit from Switchable, making them compatible with any switch.

class LightBulb(Switchable):
    def turn_on(self):
        print("LightBulb: on")

    def turn_off(self):
        print("LightBulb: off")

class Fan(Switchable):
    def turn_on(self):
        print("Fan: spinning")

    def turn_off(self):
        print("Fan: stopped")

3. The Decoupled Power Switch

The no longer cares if it's holding a bulb or a fan; it only cares that the object is Switchable.

class PowerSwitch:
    def __init__(self, client: Switchable):
        self.client = client
        self.on = False

    def press(self):
        if self.on:
            self.client.turn_off()
            self.on = False
        else:
            self.client.turn_on()
            self.on = True

Syntax Notes

The @abstractmethod decorator is a safeguard. If you inherit from ABC but forget to implement a decorated method, Python raises a TypeError the moment you try to instantiate the subclass. This enforces a strict contract between your components.

Tips & Gotchas

Always use type hints to signal your intent to other developers. While the Python interpreter ignores them, they are invaluable for documentation. A common mistake is trying to instantiate the abstract class itself; remember, Switchable is a blueprint, not a concrete object. Use this pattern whenever you find yourself writing code that is too tightly coupled to a specific implementation.

Topic DensityMention share of the most discussed topics 路 9 mentions across 9 distinct topics
11%libraries
11%software development
11%classes
11%classes
11%software development
Other topics
44%
End of Article
Source video
Mastering Dependency Inversion in Python with Abstract Base Classes

Dependency Inversion: Write BETTER PYTHON CODE Part 2

Watch

ArjanCodes // 9:03

On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!

What they talk about
AI and Agentic Coding News
Who and what they mention most
Python
33.3%5
Python
20.0%3
Python
20.0%3
Pydantic
13.3%2
3 min read0%
3 min read