Python Dependency Injection Framework

后端 未结 18 2167
你的背包
你的背包 2020-12-12 14:10

Is there a framework equivalent to Guice (http://code.google.com/p/google-guice) for Python?

相关标签:
18条回答
  • 2020-12-12 14:25

    If you want a guice like (the new new like they say), I recently made something close in Python 3 that best suited my simple needs for a side project.

    All you need is an @inject on a method (__init__ included of course). The rest is done through annotations.

    from py3njection import inject
    from some_package import ClassToInject
    
    class Demo:
        @inject
        def __init__(self, object_to_use: ClassToInject):
            self.dependency = object_to_use
    
    demo = Demo()
    

    https://pypi.python.org/pypi/py3njection

    0 讨论(0)
  • 2020-12-12 14:28

    There is a somewhat Guicey python-inject project. It's quite active, and a LOT less code then Spring-python, but then again, I haven't found a reason to use it yet.

    0 讨论(0)
  • 2020-12-12 14:31

    I recently released a neat (IMHO) micro library for DI in python:

    https://github.com/suned/serum

    0 讨论(0)
  • 2020-12-12 14:33

    If you prefer a really tiny solution there's a little function, it is just a dependency setter.

    https://github.com/liuggio/Ultra-Lightweight-Dependency-Injector-Python

    0 讨论(0)
  • 2020-12-12 14:37

    Enterprython is a small framework providing dependency-injection, building the object graph automatically based on type hints.

    0 讨论(0)
  • 2020-12-12 14:38

    I like this simple and neat framework.

    http://pypi.python.org/pypi/injector/

    Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.

    That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalized code through the use of Module s.

    While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.

    0 讨论(0)
提交回复
热议问题