[CentralOH] Singleton Revised

John Santiago jdsantiagojr at gmail.com
Mon Mar 18 05:05:17 CET 2013


Sorry sent old one, here is new singleton. I am seeing a lot of examples.
Just would liek some critique on this one

import threading
from cherrypy._cpwsgi_server import CPWSGIServer


class Singleton(object):
    __instance = None
    __lockInstance = threading.RLock()

    def __new__(cls):
        raise TypeError("Error: Singletons may only be instantiated through
getInstance()")

    def __init__(self):
        print 'hi'

    @classmethod
    def getInstance(cls, *args, **kwargs):
        if cls.__instance is None:
            try:
                cls.__lockInstance.acquire()
                cls.__instance = object.__new__(cls)
                cls.__instance.__init__(*args, **kwargs)
            finally:
                cls.__lockInstance.release()

        return cls.__instance


if __name__ == '__main__':
    s1 = Singleton.getInstance()
    s2 = Singleton.getInstance()
    s3 = Singleton.getInstance()
    s4 = Singleton.getInstance()
    s5 = Singleton()

-- 
This electronic message is intended to be for the use only of the named
recipient, and may contain information that is confidential or privileged.
If you are not the intended recipient, you are hereby notified that any
disclosure, copying, distribution or use of the contents of this message is
strictly prohibited.  If you have received this message in error or are not
the named recipient, please notify us immediately by contacting the sender
at the electronic mail address noted above, and delete and destroy all
copies of this message.  Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20130318/0a924853/attachment.html>


More information about the CentralOH mailing list