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

19 lines
603 B
Python
Raw Normal View History

"""`DelegatedSingleton` providers example."""
2016-06-06 08:54:05 +00:00
import dependency_injector.providers as providers
# Some delegated singleton provider:
singleton_provider = providers.DelegatedSingleton(object)
registry = providers.DelegatedSingleton(dict,
object1=singleton_provider,
object2=singleton_provider)
# Getting several references to singleton object:
registry = registry()
singleton_object1 = registry['object1']()
singleton_object2 = registry['object2']()
# Making some asserts:
assert singleton_object1 is singleton_object2