TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Dependency Injection for Python

37 pointsby macealmost 12 years ago

10 comments

naileralmost 12 years ago
I don&#x27;t get dependency injection. I come from languages that I am told don&#x27;t have it. But when I read the Wikipedia page for DI, I get:<p>&quot;a dependent consumer<p>a declaration of a component&#x27;s dependencies, defined as interface contracts<p>an injector (sometimes referred to as a provider or container) that creates instances of classes that implement a given dependency interface on request.&quot;<p>Wait, I still have to declare dependencies, just like the import statement in python, require.js in JS, etc. which are apparently not &#x27;dependency injection&#x27; systems? And this just adds, er more code?<p>That same wikipedia page mentions dynamically loaded dependencies while also mentioning &#x27;a declaration of a components dependencies&#x27; which sure doesn&#x27;t sound dynamic to me.<p>Does anyone care to provide a better explanation of what dependency injection gives me over the existing common dependency mechanisms?
评论 #5951247 未加载
评论 #5951218 未加载
评论 #5951280 未加载
shadowmintalmost 12 years ago
<i>sigh</i><p>DI is not the same as an IOC container. Get it right ffs, if you want people to take you seriously.<p>DI:<p><pre><code> class Blah(): def __init__(self, dependency): self.dep = depedency </code></pre> IOC:<p><pre><code> my_blah = container.resolve(Blah) </code></pre> Anyway, onto my main complaint:<p><pre><code> &quot;Pinject is a pythonic dependency injection library. Python ports of other libraries, like Spring or Guice, retain the feel (and verbosity) of being designed for a statically typed language. Pinject was designed from the ground up for python.&quot; </code></pre> Wow, I&#x27;m impressed. Designed from the ground up for python. That&#x27;s why you needed ~20 pages of documentation on how to use it?<p>Intuitive.<p>Also, what&#x27;s wrong with annotations? I&#x27;m rather fond of this sort of code:<p><pre><code> ### implements, resolve, Scope class IType(object): prop1 = None def call(self): pass @implements(IType) class ImplType(object): prop1 = &quot;Value&quot; def call(self): pass scope = Scope() scope.register(ImplType) @resolve(scope) class UsesType(object): def __init__(self, t=IType): self.t = t instance = UsesType() </code></pre> Or in pinject:<p><pre><code> obj_graph = pinject.new_object_graph(binding_specs=[SomeBindingSpec()]) needs_provider = obj_graph.provide(NeedsProvider) </code></pre> But wait, I want a test. Ok~<p><pre><code> instance = UsesType(my_mock) </code></pre> Or in pinject:<p><pre><code> ?????? </code></pre> In fact, pinject doesn&#x27;t even mention tests in its documentation. Whew~ That&#x27;s only <i>the whole reason for using DI in the first place</i>.<p>Self important library is self important. Not amazed.
评论 #5951140 未加载
评论 #5952281 未加载
hosay123almost 12 years ago
The author could refactor the documentation a little, starting with removal of the word &quot;Pythonic&quot; and perhaps ending only after 90% of the text has been replaced with a single section entitled &quot;WHY GOD WHYYYYY&quot;.<p><pre><code> def python_di_copyright_2069_hosay123(dep, *args, **kwargs): modname, _, classname = dep.rpartition(&#x27;.&#x27;) mod = importlib.import_module(modname) return getattr(mod, classname)(*args, **kwargs) </code></pre> &gt; Though Google owns this project&#x27;s copyright, this project is not an official Google product.<p>We just published it under Google&#x27;s Github to give a terrible project implied legitimacy that it likely doesn&#x27;t even have internally.
sly010almost 12 years ago
I understand dependency injection, but I never understood dependency injection frameworks. For me the whole point of DI is explicit composability. There should only be a few well defined places in my application where all my classes are assembled into a working program. If my classes cover my domain nicely, I should be able to compose all kinds of programs (including tests) and reuse code.<p>If my objects get injected to each other automatically by their name then my dependencies are just as predetermined, which results in coupled code, except the coupling is now difficult to read or modify.<p>Once I decided I am only using explicit injection, I don&#x27;t need a framework, I just instantiate my classes in the right order and pass them into each other. What can possibly be more pythonic than that?<p>I have the same issue with Angular.js btw. They are trying to make is easy to start by adding dependency injection magic, but in reality all I see is &quot;this can&#x27;t possibly work (and indeed it doesn&#x27;t)&quot;, so I just need to spend more time digging and understanding the magic before I can start writing ANY code.<p>Every documentation should start by explaining the explicit way of doing things, then adding the sugar afterwards.<p>import this
评论 #5951311 未加载
porkeralmost 12 years ago
Dependency Injection (DI) and Dependency Injection Containers (DIC) are the &quot;in-thing&quot; in PHP at present, and to speak against them [0] is - like disagreeing with anything fanboys latch onto - ridiculed, without a sensible discussion or explanation.<p>From a testing perspective I sort-of get it; from a &quot;this is less tightly coupled&quot; I don&#x27;t: all you&#x27;ve done is move the coupling to a different level. Code always has to be coupled at some level, so where&#x27;s the advantage?<p>With the amount of noise about DI I&#x27;m sure it&#x27;s something I should pay attention to rather than be turned off (as happened with Rails), but I really don&#x27;t et it. Can anyone provide the missing piece in my thinking?<p>[0] <a href="http://till.klampaeckel.de/blog/archives/154-Dependency-Injection-Containers.html" rel="nofollow">http:&#x2F;&#x2F;till.klampaeckel.de&#x2F;blog&#x2F;archives&#x2F;154-Dependency-Inje...</a>
评论 #5951924 未加载
评论 #5951683 未加载
评论 #5951704 未加载
_ZeD_almost 12 years ago
<i>Pinject is a pythonic dependency injection library.</i><p>+<p><i>Implicit class bindings</i><p>...ummm...
Myrmornisalmost 12 years ago
Implicit class bindings sound like a really bad idea and hard to debug.<p><pre><code> Pinject creates implicit bindings for classes. The implicit bindings assume your code follows PEP8 conventions: your classes are named in CamelCase, and your args are named in lower_with_underscores. Pinject transforms class names to injectable arg names by lowercasing words and connecting them with underscores. It will also ignore any leading underscore on the class name. If two classes map to the same arg name, whether those classes are in the same module or different modules, Pinject will not create an implicit binding for that arg name (though it will not raise an error).</code></pre>
antonwinteralmost 12 years ago
that looks pretty good.<p>not sure if i overlooked it in the notes or not, but i have a couple questions.<p>you mentioned copyright at the end. what sort of license are you releasing it under?<p>Does it support nested creation of objects? ( ie if you ask for an instance of class &#x27;FOO&#x27; and FOO has a DI or &#x27;BAR&#x27; will BAR be first created and then injected into FOO)
评论 #5951091 未加载
Uchikomaalmost 12 years ago
DI is about breaking constructor coupling. I wish people would understand that.
MostAwesomeDudealmost 12 years ago
Stop writing Java code in Python. No. Bad. Stop it.