PyCon UK 2019

Import as an antipattern - Demystifying Dependency Injection in modern Python
09-15, 15:00–15:30 (Europe/London), Ferrier Hall

Dependency Injection in Python is commonly seen as over-engineering, but I think this is a myth. DI is simple and powerful and can yield great benefits to the overall quality of your code.


Dependency Injection (DI) is a technique quite common in other programming languages that has never quite fitted into the Python programmer's mindset. Usually disregarded as over-engineering and complex, most of its alleged flaws have little to do with the underlying concepts but more with implementation details of large scale frameworks that obscure its core principles.

In my experience DI is simple and powerful and can greatly improve our design making our code is easier to test, extend and maintain.

In this talk I will start with a brief standard example and challenge common idioms that introduce coupling and reduce maintainability and extensibility. I will then introduce concepts like interfaces, inversion of control and DI and apply them showcasing how the design and overall quality of the code improves.

Along the way I will demonstrate how recent additions to Python and its tooling can help when applying these techniques and also address the common misconception among Python developers suggesting large complex frameworks are required to apply DI.

I will then share some examples of Dependency Injection in well known libraries and some hands-on stories where I have benefited from it in my day-to-day work. And finally I will provide some guidelines on identifying situations where DI shines and where its application might be less obvious or should be delayed until further domain knowledge is introduced.

With this talk I hope to demystify the Dependency Injection in the Python ecosystem as well as introduce and clarify important design concepts for a beginner to intermediate audience.


Is your proposal suitable for beginners? – maybe

I’m a full stack software engineer based in London, UK. I work as a freelancer helping companies develop maintainable and scalable projects.

I code primarily in Python and Javascript and have experience in Ruby, Objective-C, or C#, but most of all I love clean code.

I’m a moderator and contributor on PyPI.org, co-organizer of the London Python Meetup and also like to write about code, usually articles that help people get started on certain technologies or practices that I’ve struggled with or am passionate about.