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()
First of all, this is wrong:
An explicit
__new__method can be used like the factory pattern, so when you writeprinter(), Python will first call__new__, passing the class type and the constructor parameters (in this example none).'_instance' not in vars(cls)just means to look up whether the class already has an attribute called “_instance” (i.e. the singleton). If not, one is created. The trick withvars(builtin function, to answer your question) is not necessary, one could also usecls._instanceand catchAttributeError, for example.And the last thing:
This just creates a
printerinstance (usingSingleton.__new__) if the script is executed directly (i.e. if__name__ == '__main__', not if the module is imported).