I have the following Python code:
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class SingletonClass(metaclass=Singleton):
def __init__(self, i, j):
self.i = i
self.j = j
def out(self):
print(i)
print(j)
def __call__(self,i,j) :
print("call_SingletonClass")
self.i=i
self.j=j
x = SingletonClass(1,2)
y = SingletonClass(1,3)
print(x == y)
when i am calling x = SingletonClass(1,3)
what exactly happens ? and how the __call__
function is triggered from the Singleton
class and the call_ method not triggered from the SingletonClass Class ?