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

20 lines
542 B
Python
Raw Normal View History

"""`Singleton` providers example."""
2016-06-08 17:46:40 +03:00
import collections
2016-06-06 11:54:05 +03:00
import dependency_injector.providers as providers
2016-06-08 17:46:40 +03:00
UsersService = collections.namedtuple('UsersService', [])
# Singleton provider creates new instance of specified class on first call
# and returns same instance on every next call.
2016-06-08 17:46:40 +03:00
users_service_provider = providers.Singleton(UsersService)
# Retrieving several UserService objects:
2016-06-08 17:46:40 +03:00
users_service1 = users_service_provider()
users_service2 = users_service_provider()
# Making some asserts:
2016-06-08 17:46:40 +03:00
assert users_service1 is users_service2