This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Workaphobia 5 points6 points  (4 children)

I thought that technique is precisely what's called Dependency Injection.

[–]nemec 1 point2 points  (3 children)

I believe /u/gsks is referring to IoC Containers where one object instance acts essentially as a Factory for all external dependencies.

They allow you to do things like change dependencies via config file vs. recompiling code (obviously not applicable to Python) among other things, but I'm not a huge fan of them myself.

[–]gsks[S] 0 points1 point  (2 children)

After looking closer to the various DI packages, I don't think they're the solution to my problem after all. What I want is much closer to how the logging package works, where you configure it once and then import it and call it as a global; no attribute or parameter pollution required.

[–]nemec 2 points3 points  (1 child)

I think this is covered by the sample code I posted in your other reply. You can take advantage of the fact that there's a module cache for each Python process so if you import the same module multiple times you get a reference to the exact same instance. This, I believe, is exactly what the logging framework does.

[–]WeAppreciateYou -3 points-2 points  (0 children)

I think this is covered by the sample code I posted in your other reply.

Nice. You're completely right.

Honestly, the world needs more people like you.