mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-27 12:04:00 +03:00
22 lines
608 B
Python
22 lines
608 B
Python
"""`Singleton` providers example."""
|
|
|
|
from dependency_injector.providers import Singleton
|
|
|
|
|
|
class UserService(object):
|
|
|
|
"""Example class UserService."""
|
|
|
|
# Singleton provider creates new instance of specified class on first call and
|
|
# returns same instance on every next call.
|
|
users_service_provider = Singleton(UserService)
|
|
|
|
# Retrieving several UserService objects:
|
|
user_service1 = users_service_provider()
|
|
user_service2 = users_service_provider()
|
|
|
|
# Making some asserts:
|
|
assert user_service1 is user_service2
|
|
assert isinstance(user_service1, UserService)
|
|
assert isinstance(user_service2, UserService)
|