mirror of
https://github.com/ShutdownRepo/dcshadow
synced 2026-06-06 16:44:32 +00:00
15 lines
630 B
Python
15 lines
630 B
Python
# Generic singleton class from the Exegol project
|
|
from typing import Dict
|
|
|
|
|
|
class MetaSingleton(type):
|
|
"""Metaclass to create a singleton class"""
|
|
__instances: Dict[type, object] = {}
|
|
|
|
def __call__(cls, *args, **kwargs) -> object:
|
|
"""Redirects each call to the current class to the corresponding single instance"""
|
|
if cls not in MetaSingleton.__instances:
|
|
# If the instance does not already exist, it is created
|
|
MetaSingleton.__instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
|
|
# Return the desired object
|
|
return MetaSingleton.__instances[cls] |