In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
What are the knowledge points of this article "what are the ways to achieve the singleton pattern in Python?" most people do not understand, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the ways to achieve singleton pattern in Python?"
Summary: singleton mode ensures that there is only one instance of a class and provides a global access point to it. Applicable when there can be only one instance of a class and customers can access it from a well-known access point, such as database, MQ, and so on.
Implementation method:
1. Realized by importing module
2. Realized by decorator.
3. Through the use of classes
4. Realized by _ _ new__ method
Source code to be imported in singleton module mode: singleton.py
#-*-coding: utf-8-*-# time: 2022-5-17 10file: singleton.py# author: tom# official account: play Test Development class Singleton (object): def _ init__ (self, name): self.name = name def run (self): print (self.name) s = Singleton ("Tom")
Main function source code:
#-*-coding: utf-8-*-# time: 2022-5-17 10 file 5: test_singleton.py# author: tom# official account: play test development from singleton import s as s1from singleton import s as slots Method One: implement def show_method_one () through import module: "": return: "" print (S1) print (S2) print (id (S1)) print (id (S2)) show_method_one () # Method Two: implement def singleton (cls) through decorator: # create a dictionary to hold the instance object of the class _ instance = {} def _ singleton (* args) * * kwargs): # first determine whether the class has an object if cls not in _ instance: _ instance[ CLS] = cls (* args, * * kwargs) # create an object and save it in the dictionary # return the instance object to return _ instance[ cls] return _ singleton@singletonclass Demo2 (object): a = 1 def _ init__ (self) ): self.x = xa1 = Demo2 (1) a2 = Demo2 (2) print (id (A1)) print (id (a2)) # Method Three: class Demo3 (object) through the use of classes: # static variable _ instance = None _ flag = False def _ new__ (cls, * args * * kwargs): if cls._instance is None: cls._instance = super (). _ _ new__ (cls) return cls._instance def _ _ init__ (self): if not Demo3._flag: Demo3._flag = Trueb1 = Demo3 () b2 = Demo3 () print (id (b1)) print (id (b2)) # Method Four: via _ _ new__ Method to realize class Demo4: def _ new__ (cls) * args, * * kwargs): if not hasattr (cls,'_ instance'): cls._instance = super (Demo4, cls). _ _ new__ (cls) return cls._instancec1 = Demo4 () c2 = Demo4 () print (id (C1)) print (id (c2))
Running result:
The above is about the content of this article on "what are the ways to achieve the singleton model in Python?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.