Injector.py File
: Marks constructors or methods that need dependencies automatically provided.
Use type hints to declare what a class needs. The framework uses these hints to "look up" the correct dependency.
: Use the @provider decorator inside a Module for more complex object creation that requires logic. injector.py
: Defines "bindings"—the rules for how an interface (like an abstract class) maps to a specific implementation.
from injector import inject class Database: def __init__(self): self.status = "Connected" class Service: @inject def __init__(self, db: Database): self.db = db Use code with caution. Copied to clipboard : Marks constructors or methods that need dependencies
from injector import Injector injector = Injector([MyModule()]) # Pass your modules here service = injector.get(Service) # Automatically creates Database and Service print(service.db.status) # "Connected" Use code with caution. Copied to clipboard
: Control object lifetimes. Common scopes include singleton (one instance for the whole app) and NoScope (new instance every time). : Use the @provider decorator inside a Module
from injector import Module, singleton class MyModule(Module): def configure(self, binder): # Always provide the same instance (Singleton) binder.bind(Database, to=Database(), scope=singleton) Use code with caution. Copied to clipboard