---
title: "What is dependency injection in python?"  
description: "What is dependency injection in python?"  
author: "Ashutosh Patel"  
published: 2025-03-24  
updated: 2025-03-25  
canonical: https://www.mindstick.com/forum/161338/what-is-dependency-injection-in-python  
category: "python"  
tags: ["python"]  
reading_time: 2 minutes  

---

# What is dependency injection in python?

What is [dependency injection](https://www.mindstick.com/articles/335832/services-and-dependency-injection-in-angularjs)?

## Replies

### Reply by Khushi Singh

\
**[Dependency](https://www.mindstick.com/interview/23013/what-is-dependency-resolution) [Injection](https://www.mindstick.com/news/2203/merck-pays-250-million-to-moderna-for-a-customised-cancer-vaccine) (DI)** serves as a design pattern through [Python](https://www.mindstick.com/blog/33372/10-reasons-why-you-should-learn-python) and other programming languages to enhance code modernization, together with maintenance capabilities and testing capabilities. The dependency injection process requires external objects or services to enter a class instead of using internal class instantiation. The code base becomes more flexible while becoming easier to modify because of this dependency structure, so it is also ready for unit testing.

## How Dependency Injection Works

The dependencies required by the class are entered through arguments during initialization or method invocation instead of being generated inside the class. The approach complies with the Inversion of Control (IoC) principle, which allows an external framework or other class to control dependency life cycles.

## Example Without Dependency Injection (Tightly Coupled Code)

```python
class EmailService:
   def send_email(self, message):
       print(f"Sending email: {message}")
class Notification:
   def __init__(self):
       self.email_service = EmailService()  # Dependency created inside the class
   def notify(self, message):
       self.email_service.send_email(message)
notifier = Notification()
notifier.notify("Hello, User!")
```

The Notification class creates `EmailService` instances directly, which results in tight coupling that creates challenges for testing and modification.

## Example With Dependency Injection (Loosely Coupled Code)

```python
class EmailService:
   def send_email(self, message):
       print(f"Sending email: {message}")
class Notification:
   def __init__(self, email_service):  # Dependency is injected
       self.email_service = email_service
   def notify(self, message):
       self.email_service.send_email(message)
email_service = EmailService()
notifier = Notification(email_service)  # Inject dependency
notifier.notify("Hello, User!")
```

The new version implements dependency injection by providing `EmailService` to Notification, which creates more flexibility during testing and maintenance.

## Advantages of Dependency Injection in Python

- The testing process becomes more effective through Dependency Injection since developers can create fake dependency objects, which help during unit testing.
- The ability to replace dependencies becomes seamless while the main program logic stays untouched.
- The practice enables components to be utilized across multiple contexts while avoiding any changes to their structure.
- From an extension perspective, classes stand independent from what implementation they need, thus making them expandable.

Learn more about [why Python has future trends](https://www.mindstick.com/blog/245961/why-python-has-future-trend)


---

Original Source: https://www.mindstick.com/forum/161338/what-is-dependency-injection-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
