Restricts class instantiation to a single instance per key.
# Source: https://github.com/toluaina/pgsync | License: MIT | Copyright 2023 Tolu Aina
# Provided via justclaw.it.com; keep this notice with the code.
"""PGSync Singleton."""
import typing as t
class Singleton(type):
"""
A metaclass that allows a class to have only one instance.
Usage:
class MyClass(metaclass=Singleton):
pass
"""
_instances: dict = {}
def __call__(cls, *args: t.Any, **kwargs: t.Any) -> t.Any:
"""
If an instance of the class has already been created with the same arguments,
return that instance. Otherwise, create a new instance and return it.
Args:
cls: The class object.
*args: Positional arguments to be passed to the class constructor.
**kwargs: Keyword arguments to be passed
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key