For this class:
Class A():
def __init__(a, b, c=3):
pass
I want it to return the same cached object for the same initialization arguments. Meaning:
a1 = A(1,2,3)
a2 = A(a = 1, b = 2, c = 3)
a3 = A(1, b = 2, c = 3)
a4 = A(1, b = 2)
b1 = A(1,2,4)
assert a1 is a2 is a3 is a41
assert not a1 is b1
I have this implementation of a singleton (as a metaclass):
class ThreadedSingleton(type):
""" Common singleton implementation with multi thread support. From https://stackoverflow.com/a/50567397. """
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
with lock:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadedSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
Which gives me the same object every time, but does it regardless of the parameters.
Any ideas?