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

24 lines
466 B
Python
Raw Normal View History

"""`Singleton` provider resetting example."""
from dependency_injector import containers, providers
2016-06-08 17:46:40 +03:00
class UserService:
...
class Container(containers.DeclarativeContainer):
2021-02-06 02:13:05 +03:00
user_service = providers.Singleton(UserService)
if __name__ == '__main__':
container = Container()
2021-02-06 02:13:05 +03:00
user_service1 = container.user_service()
2021-02-06 02:13:05 +03:00
container.user_service.reset()
2021-02-06 02:13:05 +03:00
user_service2 = container.user_service()
2020-09-04 00:57:26 +03:00
assert user_service2 is not user_service1