Files
Python-With-Gtk-Template/src/libs/singleton.py
T

33 lines
558 B
Python
Raw Normal View History

2023-03-27 19:53:47 -05:00
# Python imports
2026-01-12 23:34:34 -06:00
from typing import Type, TypeVar, Any
2023-03-27 19:53:47 -05:00
# Lib imports
# Application imports
2023-03-27 21:25:54 -05:00
2023-03-27 19:53:47 -05:00
class SingletonError(Exception):
pass
T = TypeVar('T', bound = 'Singleton')
2026-01-12 23:34:34 -06:00
2023-03-27 19:53:47 -05:00
class Singleton:
_instances = {}
2023-03-27 19:53:47 -05:00
2026-01-12 23:34:34 -06:00
def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T:
if cls in cls._instances: return cls._instances[cls]
2025-12-28 19:53:05 -06:00
instance = super().__new__(cls)
cls._instances[cls] = instance
return instance
2025-12-28 19:53:05 -06:00
@classmethod
def destroy(cls):
if cls in cls._instances:
del cls._instances[cls]