-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
28 lines (19 loc) · 858 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Singleton(type):
"""Metaclass implementing the Singleton pattern.
This metaclass ensures that only one instance of a class is created and shared among all instances.
Attributes:
_instances (dict): Dictionary holding the unique instances of each class.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
"""Overrides the call behavior when creating an instance.
This method checks if an instance of the class already exists. If not, it creates a new instance and
stores it in the _instances dictionary.
Args:
cls (type): Class type.
Returns:
object: The instance of the class.
"""
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]