Skip to main content

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

from inversify import Container, injectable, inject_params

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_params({'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

Metadata

Release files for pynversify 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pynversify 1.0.2
File Size Uploaded
pynversify-1.0.2.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pynversify 1.0.2
File Interpreter ABI Platform
pynversify-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.2 kB

Release files / pynversify-1.0.2.tar.gz

Download URL pynversify-1.0.2.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f22e7f56152237bcc7e27bc3c8974d54876602e91b7b17d5b32acae8fcc66f6f
BLAKE2b-256 checksum
How to use checksums
50898cf7eda5e93e52f2adf95108898a859db9897c9759b9bb9736b284f2479b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / pynversify-1.0.2-py3-none-any.whl

Download URL pynversify-1.0.2-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12d295398e0086b1aec6f6050171e00ffceb931ae7619693aeb8efa9b90b9d75
BLAKE2b-256 checksum
How to use checksums
641e67420a7e144c4a70820c84e922f601b64243ce453b5878603d03e01d22d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page