Python Singleton mode and Python Mode

Source: Internet
Author: User

Python Singleton mode and Python Mode

Method 1

 

Python code
  1. Import threading
  2. Class Singleton (object ):
  3. _ Instance = None
  4. _ Lock = threading. Lock () # used to synchronize code
  5. Def _ init _ (self ):
  6. "Disable the _ init _ method"
  7.  
  8. @ Staticmethod
  9. Def getInstance ():
  10. If not Singleton. _ instance:
  11. Singleton. _ lock. acquire ()
  12. If not Singleton. _ instance:
  13. Singleton. _ instance = object. _ new _ (Singleton)
  14. Object. _ init _ (Singleton. _ instance)
  15. Singleton. _ lock. release ()
  16. Return Singleton. _ instance

1. Disable the _ init _ method. You cannot directly create an object.

2. _ instance: the singleton object is private.

3. @ staticmethod: static method, called directly by class name.

4. _ lock: code lock.

5. inherit from the object class, create a singleton object by calling the _ new _ method of the object, and call the complete initialization of the _ init _ method of the object.

6. Double check and locking can not only achieve thread security, but also ensure high performance.

 

Method 2: decorator

 

Python code
  1. # Encoding = UTF-8
  2. Def singleton (cls ):
  3. Instances = {}
  4. Def getInstance ():
  5. If cls not in instances:
  6. Instances [cls] = cls ()
  7. Return instances [cls]
  8. Return getInstance
  9.  
  10. @ Singleton
  11. Class SingletonClass:
  12. Pass
  13. If _ name _ = '_ main __':
  14. S = SingletonClass ()
  15. S2 = SingletonClass ()
  16. Print s
  17. Print s2

 

Thread security should also be added

 

 

Appendix: High Performance

 

Python code
  1. Import threading
  2. Class Sing (object ):
  3. Def _ init __():
  4. "Disable the _ init _ method"
  5. _ Inst = None # make it so-called private
  6. _ Lock = threading. Lock () # used to synchronize code
  7.  
  8. @ Staticmethod
  9. Def getInst ():
  10. Sing. _ lock. acquire ()
  11. If not Sing. _ inst:
  12. Sing. _ inst = object. _ new _ (Sing)
  13. Object. _ init _ (Sing. _ inst)
  14. Sing. _ lock. release ()
  15. Return Sing. _ inst

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.