You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
402 B
Python
19 lines
402 B
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Type, TypeVar
|
|
|
|
|
|
T = TypeVar("T", bound="Singleton")
|
|
|
|
|
|
class Singleton(type):
|
|
_instances = dict[Type, Any]()
|
|
|
|
def __call__(cls: T, *args, **kwargs) -> T:
|
|
if cls not in cls._instances:
|
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
return cls._instances[cls]
|
|
|
|
|
|
__all__ = ["Singleton"]
|