Skip to content

100nm/python-injection

Repository files navigation

python-injection

CI PyPI - Version PyPI - Downloads Ruff

Documentation: https://python-injection.remimd.dev

Installation

⚠️ Requires Python 3.12 or higher

pip install python-injection

Quick start

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()