Skip to content

Instantly share code, notes, and snippets.

@curtiscook
curtiscook / singleton.py
Created December 8, 2022 03:02
Python 3 - Singleton Access Pattern
import os
from typing import Any, Dict
class Singleton(type):
_instances: Dict[Any, Any] = {}
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)