May-29-2019, 03:55 PM
Hi!
I'm not too sure why the following code is working:
Usually a class decorator returns another class.
I'm not too sure why the following code is working:
class Statics(): lock = threading.Lock() def thread_safe(func): def inner(*args,**kwargs): with Statics.lock: result = func(*args, **kwargs) return result return inner @thread_safe def singleton(class_): instances = {} def getinstance(*args, **kwargs): if class_ not in instances: instances[class_] = class_(*args, **kwargs) return instances[class_] return getinstanceSpecifically,the singleton decorator,which decorates a class,is working, but I don't understand why.
Usually a class decorator returns another class.