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

30 lines
623 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
class SingletonError(Exception):
pass
2026-01-12 23:34:34 -06:00
T = TypeVar('T', bound='SingletonRaised')
class SingletonRaised:
__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:
raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...")
cls.__instance = super(SingletonRaised, 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