23 lines
520 B
Raw Permalink Normal View History

Singleton decorator definition
from functools import wraps
def Singleton(cls):
Decorator implementing the singleton pattern:
it restricts the instantiation of a class to one "single" instance.
instances = {}
def getinstance():
"""Find an instance or save newly created one"""
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance