"""Container traversal example.""" from dependency_injector import containers, providers def init_database(): return ... def init_cache(): return ... class Service: def __init__(self, database, cache): self.database = database self.cache = cache class Container(containers.DeclarativeContainer): config = providers.Configuration() service = providers.Factory( Service, database=providers.Resource( init_database, url=config.database_url, ), cache=providers.Resource( init_cache, hosts=config.cache_hosts, ), ) if __name__ == "__main__": container = Container() for provider in container.traverse(): print(provider) # # ) at 0x10d3a2820> # ) at 0x10d346b40> # # ) at 0x10d346bc0> #