skip to navigation
skip to content

Not Logged In

pythonioc 0.2.6

simplistic dependency injection container for python

Latest Version: 0.3.1

This package provides a simple inversion-of-control container.

Install with

pip install pythonioc

Quick Start

The main idea is: services are registered to a service-registry and can be injected into users of that service (which can of course be services themselves).

You have two options:

  1. use a global registry (never create a registry yourself)

    –> easy to implement, harder to test

  2. use a local registry

Examples are below, more details will follow.

Global Registry

import pythonioc

# register your service
class SomeService(object):

    # called when the service is auto-instantiated.
    def postInit(self):

class DifferentService(object):

# for classes which we cannot decorate:

# when we don't even have the class (or don't care about lazy-initialization)

class ServiceUser(object):
    # inject the dependency by class
    service = pythonioc.Inject(SomeService)

    # inject the dependency by name (for cyclic dependencies)
    service2 = pythonioc.Inject('DifferentNameService')

 myUser = ServiceUser()

 myUser.service # --> automatically created and injected service instance.

 # explicitly get a services

Local Registry

class Service(object):

    # this will make the service registry inject a service named "someOtherService" which
    # comes from class SomeOtherService
    _someOtherService = None

    def __init__(self):

    # will be called after everything is injected
    def postInit(self):

    # will be called right before the object is destroyed (the registry's clean
    # method is called)
    def preDestroy(self):

class SomeOtherService(object):

# Use it like

reg = ServiceRegistry()

Once the system has all required services registered, a service can be injected by doing

class WiredUser(object):


    def __init__(self, *args):

wiredUser = reg.createWired(WiredUser, 'arg1', 'arg2')

Wired objects are not automatically part of the service registry, only if added by calling reg.registerServiceInstance.

Wired objects can inject its own service registry, so they can created wired Objects on the fly:

class WiredUser(object):

class UserCreator(object):

    def createUser(self):
        return self._serviceRegistry.createWired(WiredUser)

userCreator = reg.createWired(UserCreator)

# create some wired users
userA = userCreator.createUser()
userB = userCreator.createUser()


  • lazy initialization of services
  • dependency cycle detection

Notes on Names

Services added to the registry need a name. If no name is provided, the class’ name (or the instance’s class’ name) is used. The name’s first character is lowered.


import pythonioc

class MyService(object):

pythonioc.registerService(MyService)            # --> name is 'myService'
pythonioc.registerServiceInstance(MyService())  # --> name is 'myService'
pythonioc.registerService(MyService, serviceName='customName') # --> name is 'customName'
pythonioc.registerServiceInstance(MyService(), serviceName='customName2')  # --> name is 'customName2'

Feel free to report any bugs, suggest improvements etc.

File Type Py Version Uploaded on Size
pythonioc-0.2.6.tar.gz (md5) Source 2014-09-04 4KB
  • Downloads (All Versions):
  • 2 downloads in the last day
  • 113 downloads in the last week
  • 176 downloads in the last month