python-dependency-injector/examples/providers/dependency_default.py

26 lines
486 B
Python
Raw Normal View History

"""`Dependency` provider default example."""
import abc
from dependency_injector import containers, providers
class Cache(metaclass=abc.ABCMeta):
...
class InMemoryCache(Cache):
...
class Container(containers.DeclarativeContainer):
cache = providers.Dependency(instance_of=Cache, default=InMemoryCache())
2021-09-30 22:32:21 +03:00
if __name__ == "__main__":
container = Container()
cache = container.cache() # provides InMemoryCache()
assert isinstance(cache, InMemoryCache)