def good_singleton(class_: Type) -> Callable[..., Any]:
"""
Singleton decorator. If instance of class already exists, raises ValueError.
It blocks creating new instances of classes that should instantiate only once.
"""
instances: Dict[Type, Any] = {}
def getinstance(*args, **kwargs) -> Any:
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
raise ValueError(f"Instance of {class_} already exists")
return getinstance
Я изобрел хороший синглтон!
Веткаdef good_singleton(class_: Type) -> Callable[..., Any]:
4 сообщения · –T T