许多小伙伴面试的时候,会被频繁的问道手写单例模式,尤其是再加上装饰器,就更晕了。下面听过一串代码来实现,仅供参考。如有错误,敬请指正!
def Singleton(cls):
instances = {}
def get_instance(*args, **kw):
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return get_instance