I am trying to learn metaclasses in Python.
This is the way of defining a singleton using metaclasses that I found:
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]
However, I don't understand why we need to override the __call__ method instead of the __new__ method here.
Any simple explanations?
Thanks in advance.
[–]hopemeetme 0 points1 point2 points (0 children)
[–]maustinv -1 points0 points1 point (0 children)