https://github.com/fdorantesm/pynversify
A dependency injection container inspired by Inversify for Python.
https://github.com/fdorantesm/pynversify
dependency-injection dependency-injection-container dependency-inversion dependency-inversion-principle
Last synced: 24 days ago
JSON representation
A dependency injection container inspired by Inversify for Python.
- Host: GitHub
- URL: https://github.com/fdorantesm/pynversify
- Owner: fdorantesm
- Created: 2025-02-28T06:19:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-03T06:01:47.000Z (over 1 year ago)
- Last Synced: 2026-04-01T14:15:57.838Z (4 months ago)
- Topics: dependency-injection, dependency-injection-container, dependency-inversion, dependency-inversion-principle
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Inversify for Python
A dependency injection container inspired by Inversify for Python.
This library supports hierarchical containers, various binding types (toSelf, to, constant, dynamic),
and scopes (singleton and transient).
## Examples
```py
from inversify import Container, injectable, inject
class Token:
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return isinstance(other, Token) and self.name == other.name
# Tokens for bindings
LOGGER_TOKEN = Token("LOGGER")
USER_SERVICE_TOKEN = Token("USER_SERVICE")
@injectable
class Logger:
def __init__(self):
self.name = "Parent Logger"
def log(self, message: str):
print(f"[{self.name}]: {message}")
@injectable
class CustomLogger:
def __init__(self):
self.name = "Child Logger"
def log(self, message: str):
print(f"[{self.name}]: {message}")
@injectable
class UserService:
@inject({'logger': LOGGER_TOKEN})
def __init__(self, logger):
self.logger = logger
def process(self):
self.logger.log("Processing in UserService")
# Create parent container and bind tokens
parent_container = Container()
parent_container.bind(LOGGER_TOKEN).to(Logger).inSingletonScope()
parent_container.bind(USER_SERVICE_TOKEN).to(UserService).inTransientScope()
# Create child container and override the LOGGER_TOKEN binding
child_container = parent_container.create_child()
child_container.bind(LOGGER_TOKEN).to(CustomLogger).inSingletonScope()
# Get services from parent and child containers
parent_service = parent_container.get(USER_SERVICE_TOKEN)
child_service = child_container.get(USER_SERVICE_TOKEN)
parent_service.process() # Uses Parent Logger
child_service.process() # Uses Child Logger
```