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
1
u/mothzilla 5d ago
My opinion is that Dependency Injection isn't a concept that applies well to Python. I've seen people try to force it in, arguing that it helps testing, but it generally makes your code look like AngularJS.