my first question here:
I've searched the internet and also read several question and answers here before and finally figured out the way of writing the singleton classes for my python codes. I've also read the python documentation about the new() function and some other things but still confused about how and what is mean by all that new(cls, *args, **kw) things etc!
for example I wrote a test code like this:
class Singleton(object):
def __new__(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
class printer(Singleton):
def __init__(self):
print "I am a new Object, and will remain until the end of time!"
if __name__ == '__main__':
printer()
and the result is the text "I am a new Object, and will remain until the end of time!" But how this works, I mean, I don't know how to tell, for example I'm really confused about:
vars(cls): in the line if '_inst' not in vars(cls)
from where the vars(cls) comes out, I didn't declare that before! Can someone please clarify this things in the singleton class for me plus some about the last line
if __name__ == '__main__':
printer()