I found this searching for singleton concepts in Python. What I wonder about is why self._instance = super(Singleton, self).__new__(self)
doesn't cause an infinte loop. I thought that calling __new__
would start a kind of recursion, as self._instance
should not have been set then.
Where's my mistake?
class Singleton(object):
_instance = None
def __new__(self):
if not self._instance:
self._instance = super(Singleton, self).__new__(self)
return self._instance