python-dependency-injector/examples/containers/reset_singletons_subcontainers.py
Roman Mogylatov c964253204
Container.reset_singletons() (#390)
* Rename container tests

* Add implementation + tests

* Update changelog

* Add examples and docs
2021-02-05 17:14:10 -05:00

27 lines
619 B
Python

"""Container reset singletons in subcontainer example."""
from dependency_injector import containers, providers
class SubContainer(containers.DeclarativeContainer):
service = providers.Singleton(object)
class Container(containers.DeclarativeContainer):
service = providers.Singleton(object)
sub = providers.Container(SubContainer)
if __name__ == '__main__':
container = Container()
service1 = container.service()
service2 = container.sub().service()
container.reset_singletons()
assert service1 is not container.service()
assert service2 is not container.sub().service()