18 lines
552 B
Python
18 lines
552 B
Python
|
def singleton(cls):
|
||
|
"""
|
||
|
Decorator for making a class a singleton.
|
||
|
This ensures that only one instance of the class exists.
|
||
|
"""
|
||
|
instances = {} # Dictionary to store the instance of the singleton class
|
||
|
|
||
|
def get_instance(*args, **kwargs):
|
||
|
"""
|
||
|
If an instance of the class does not exist, create one and store it.
|
||
|
If it exists, return the existing instance.
|
||
|
"""
|
||
|
if cls not in instances:
|
||
|
instances[cls] = cls(*args, **kwargs)
|
||
|
return instances[cls]
|
||
|
|
||
|
return get_instance
|