/ src / singleton.py
singleton.py
 1  """
 2  Singleton decorator definition
 3  """
 4  
 5  from functools import wraps
 6  
 7  
 8  def Singleton(cls):
 9      """
10      Decorator implementing the singleton pattern:
11      it restricts the instantiation of a class to one "single" instance.
12      """
13      instances = {}
14  
15      # https://github.com/sphinx-doc/sphinx/issues/3783
16      @wraps(cls)
17      def getinstance():
18          """Find an instance or save newly created one"""
19          if cls not in instances:
20              instances[cls] = cls()
21          return instances[cls]
22      return getinstance