CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/公开课/文档/年薪50W+的Python程序员如何写代码/code/Python/opencourse/part01/example08.py
Views: 729
1
from functools import wraps
2
from threading import RLock
3
4
5
def singleton(cls):
6
instances = {}
7
lock = RLock()
8
9
@wraps(cls)
10
def wrapper(*args, **kwargs):
11
if cls not in instances:
12
with lock:
13
if cls not in instances:
14
instances[cls] = cls(*args, **kwargs)
15
return instances[cls]
16
17
18
@singleton
19
class President:
20
pass
21
22
23
President = President.__wrapped__
24
25