Imagine a system (Python) where the different parts constantly interact with one instance of a given object. What is the best way to provide a global access point to this instance?
So far I can only think of building the (Singleton) instance in __init__.py and import the module as needed:
# __init__.py
class Thing(object, Singleton):
pass
TheThing = Thing()
__all__ = ['TheThing']
Is there a better way to provide a global access point to TheThing?
Thanks,
J.
Don’t use
singletonsin python. Python modules are great singletons (they are initialized only once and are available everywhere) and you can have a global variable in one, if you need it.Here is explanation: Is there a simple, elegant way to define singletons?