| 1234567891011121314151617181920212223 |
- 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__
|