Skip to content

Comment on Dependency Injection for Pythonparent

Comments

At its core DI is basically constructing instances of objects with the help of other objects. So you have constructor injection , setter injection , property injection ,etc ...

Now that you got these huge object graphs, you need a way to wire them without pain.

That's where service locators and IOC containers are handy.

A service locator is basically a registery that holds the dependencies and you instanciate objects that way

     container.createDefinition("MyClass").injectProperty("prop1","MyOtherClass").injectConstructor("MyThirdClass","MyLastClass")
then
    myInstance = container.create("MyClass")

or whatever

The question is while dependency injection is good practice in general , do Pythonists need IOC containers or Service Locators? etc ...

AMD in javascript has nothing to do with dependency injection. AMD is not about instanciating objects but resolving file dependencies.

"At its core DI is basically constructing instances of objects with the help of other objects."

Why would you wan t that in python? Doesn't python have higher order functions?

Exactly, in a dynamic language with first-class functions you don't need to create object factories just to pass around static methods between scopes.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.