Python Singleton ;__ init _ call __;__ metaclass __; Python Singleton

Source: Internet
Author: User

1. There are many implementation methods on the Internet, and the stackflow contains various implementation details. Google by yourself, so you don't need to post a URL.


I will briefly describe the principle here and give a simple demo to help you understand it.

  1 class Singleton(type):                                                                                                 2     def __init__(self, name, bases, dic):  3         print ".... Singleton.init ...."  4         super(Singleton, self).__init__(name, bases, dic)  5         self.instance = None  6   7     def __call__(self, *args, **kwargs):  8         print ".... Singleton.call ...."  9         if self.instance is None: 10             self.instance = super(Singleton, self).__call__(*args, **kwargs) 11         return self.instance 12  13 print "====================== 1 =======================" 14 class test: 15     __metaclass__ = Singleton #call Singleton.__init__ 16     def __init__(self): 17         print "test.init ..." 18     def __call__(self): 19         print "test.call ..." 20  21 class test2: 22     __metaclass__ = Singleton #call Singleton.__init__ 23     def __init__(self): 24         print "test2.init ..." 25     def __call__(self): 26         print "test2.call ..." 27  28 print "====================== 2 =======================" 29 a = test() #call Singleton.__call__ 30 b = test2() #call sSngleton.__call__ 31  32 print "====================== 3 =======================" 33 a1 = test() 34 b1 = test2() 35 print id(a),id(a1),id(b),id(b1) 36 print "====================== 4 =======================" 37 a1() 38 b1() 39  40 print "====================== 5 =======================" 41 a.c = 100 42 b.c = 111 43 print a1.c 44 print b1.c

Output result:

1  bash-3.2$ python test.py 2  ====================== 1 =======================3  .... Singleton.init ....4  .... Singleton.init ....5  ====================== 2 =======================6  .... Singleton.call ....7  test.init ...8  .... Singleton.call ....9  test2.init ...10 ====================== 3 =======================11 .... Singleton.call ....12 .... Singleton.call ....13 4476624848 4476624848 4476670096 447667009614 ====================== 4 =======================15 test.call ...16 test2.call ...17 ====================== 5 =======================18 10019 111




Two Singleton classes, test and test2, are created in the demo. Both use _ metaclass _ = Singleton.

Test results:

The test output shows that id (a) = id (a1), id (B) = id (b1), and create a c attribute to, a1 will also get an identical c, including the c value. The Singleton is OK.


Principle:

Here, the Singleton is implemented in the _ metaclass _ method._ Metaclass _ indicates the class of the class.

Therefore, once the _ metaclass _ = Singleton of test and test2 is set,InstanceAs the class of test and test2. (A bit closed)


Note that line 14 and line 22 correspond to line 2 and line3 printing in the output results. Note:Once we set test, test2's _ metaclass _ = Singleton, Singleton immediately _ init _, and for each Singleton class (test or test2 here) this is done only once during definition.


In the following example, test, test2: a = test (), B = test2 (); then the system will call the _ call __of Singleton; instantiate several times test, test2, call Singleton's _ call _ several times __. Therefore, we only need to implement a Singleton in Singleton's _ call.


The specific implementation method is very simple, just look at the source code.


Add:

The _ init _ function of the test class corresponds to a = test () call.

_ Call _ function corresponds to ()


Related Article

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.