24 lines
427 B
Python
24 lines
427 B
Python
from functools import wraps
|
|
from threading import RLock
|
|
|
|
|
|
def singleton(cls):
|
|
instances = {}
|
|
lock = RLock()
|
|
|
|
@wraps(cls)
|
|
def wrapper(*args, **kwargs):
|
|
if cls not in instances:
|
|
with lock:
|
|
if cls not in instances:
|
|
instances[cls] = cls(*args, **kwargs)
|
|
return instances[cls]
|
|
|
|
|
|
@singleton
|
|
class President:
|
|
pass
|
|
|
|
|
|
President = President.__wrapped__
|