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

33 lines
659 B
Python
Raw Normal View History

# Python imports
2026-01-12 23:34:34 -06:00
from typing import Type, TypeVar, Any
# Lib imports
# Application imports
2023-03-27 21:25:54 -05:00
class SingletonError(Exception):
pass
2026-01-12 23:34:34 -06:00
T = TypeVar('T', bound='Singleton')
class Singleton:
_instance = None
2026-01-12 23:34:34 -06:00
def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T:
if cls._instance is not None:
logger.debug(f"'{cls.__name__}' is a Singleton. Returning instance...")
return cls._instance
cls._instance = super(Singleton, cls).__new__(cls)
return cls._instance
2026-01-12 23:34:34 -06:00
def __init__(self) -> None:
if self._instance is not None:
return
super(Singleton, self).__init__()