25 lines
687 B
Python
25 lines
687 B
Python
|
|
from functools import wraps
|
|
|
|
def singleton(cls):
|
|
instances = {}
|
|
|
|
@wraps(cls)
|
|
def get_instance(*args, **kwargs):
|
|
instance_id = kwargs.get('instance_id')
|
|
|
|
# If instance_id provided, use localized mode (one instance per ID)
|
|
# Otherwise, use global mode (single instance)
|
|
key = (cls, instance_id) if instance_id else cls
|
|
|
|
if key not in instances:
|
|
print(f"Creating new instance with key: {key}")
|
|
instances[key] = cls(*args, **kwargs)
|
|
else:
|
|
print(f"Reusing existing instance with key: {key}")
|
|
|
|
# either way:
|
|
return instances[key]
|
|
|
|
return get_instance
|
|
|