2

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.

Escualo
  • 36,702
  • 18
  • 79
  • 122

1 Answers1

6

Don't use singletons in 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?

Community
  • 1
  • 1
gruszczy
  • 37,239
  • 27
  • 119
  • 167