python-dependency-injector/examples/containers/reset_singletons_with.py
Roman Mogylatov 2bf3601695
Singleton reset context (#417)
* Add implementation and typing stubs

* Make some refactoring and add tests

* Pin ubuntu version to 18.04

* Add docs and example

* Add changelog

* Add container docs
2021-03-03 08:28:10 -05:00

24 lines
525 B
Python

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