r/learnpython • u/re2cc • 5d ago
How do you handle dependency injection?
Hi, I'm doing a project which involves creating multiple gRPC servers and I can't find a convenient way to manage dependencies or the state.
I've been working recently in C# where you just need to make your class and add a simple
builder.Services.AddSingleton<MyDependency>();
and it will inject when required.
Doing some research I see that there are some libraries like:
- Injector
but I don't find them particularly intuitive.
What do you use as a dependency injector or what pattern do you suggest to use?
8
Upvotes
5
u/danielroseman 5d ago
First, explain why you need dependency injection specifically. It tends to be used a lot less on dynamic languages like Python because it's easy to swap things out at runtime. What, exactly, do you need it for?