| [ Web Proxy ] |
| Viewing: https://python-injection.remimd.dev | [Back] [Original] |
[PyPI - Version]
[PyPI - Downloads]
Dependency injection in Python has long been a source of frustration.
Existing solutions are often verbose, require extensive boilerplate, or fail to leverage Python's type hints effectively. python-injection was created to solve these problems once and for all by providing a simple, elegant, and powerful dependency injection framework that feels natural to Python developers.
The goal is straightforward: make dependency injection so easy that you'll wonder how you ever managed without it.
Requires Python 3.12 or higher.
Simply apply the decorators and the package takes care of the rest.
from injection import injectable, inject, singleton
@singleton
class Printer:
def __init__(self):
self.history = []
def print(self, message: str):
self.history.append(message)
print(message)
@injectable
class Service:
def __init__(self, printer: Printer):
self.printer = printer
def hello(self):
self.printer.print("Hello world!")
@inject
def main(service: Service):
service.hello()
if __name__ == "__main__":
main()
| Web Proxy Viewer | New URL | Original Page |