python-dependency-injector/examples/providers/singleton_multiple_containers.py
2020-09-03 17:57:26 -04:00

25 lines
615 B
Python

"""`Singleton` provider resetting example."""
from dependency_injector import containers, providers
class UserService:
...
class Container(containers.DeclarativeContainer):
user_service_provider = providers.Singleton(UserService)
if __name__ == '__main__':
container1 = Container()
user_service1 = container1.user_service_provider()
assert user_service1 is container1.user_service_provider()
container2 = Container()
user_service2 = container2.user_service_provider()
assert user_service2 is container2.user_service_provider()
assert user_service1 is not user_service2