TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

Dynamic, Lazy Dependency Injection in Python

Mike Huls
6 min readNov 22, 2024

--

Photo by Rapha Wilde / Unsplash

Dependency Injection (DI) solves many problems by improving testability, decoupling, maintainability and readability. However, managing dependencies can sometimes introduce new problems. When do we initialize them? How do we initialize? Can they be reused effectively?

In order to take DI to the next level I’ve created FastInject: a Python package that simplifies dependency management with just a few decorators. FastInject automatically handles dependency instantiation and injection so that you can focus on your project. Features:

  • improved performance: Only create dependencies when they are actually needed
  • simpler initialization: Dependencies are resolved dynamically
  • avoid circular imports: Dependency resolution is deferred until runtime
  • improved flexibility: Dependencies can be influenced by runtime information or configuration

Let’s code!

Contents

  1. DI refresher: A comparison of code that uses DI versus code that doesn’t
  2. Dependency management with FastInject…

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Mike Huls
Mike Huls

Written by Mike Huls

I write about interesting programming-related things: techniques, system architecture, software design and how to apply them in the best way. — mikehuls.com

Responses (1)