Today, let's talk about the use of a single object.
The singleton object pattern is a common software design pattern, which ensures that only one instantiated object of a class exists. This singleton pattern can be used when we want to have only one instance of a class in the whole program.
Use__ new__ method
Use__ new__ Method can restrict the class to be instantiated only once. Just use the following method to define it
class Singleton(object): _instance = None def __new__(cls,*args,**kwargs): if not cls._instance: cls._instance = super(Singleton,cls).__new__(cls,*args,**kwargs) return cls._instance
When we instantiate an object for this class for the first time, the class variable_ instnce will be assigned as a new object, and cls_ If instance has a value, it will not re instantiate a new object. We can judge the effect by id
a = Singleton() b = Singleton() print(id(a)) print(id(b)) ########output######## 139968970404024 139968970404024
That is to say, a and b instantiated respectively are actually one object.
Use module
Python module import is a natural singleton mode, because a module will generate a singleton when it is imported for the first time When importing the pyc file for the second time, you will first find out whether it has been poured into the pyc file. If the module has been imported before, the import process will not be performed. So we instantiate the required object in the module and import it into the required script, which is a singleton object.
We now have a script file that defines a class and instantiates it
#mysingleton.py class Animal(object): def run(self): print('running...') a = Animal()
In another script, we import the instantiated object
#main.py from mysingleton import a a.run()
Because this object a can only be imported once, if a program is composed of many script files, as long as one file imports this object a, this a is actually an object no matter in which script it is imported, even if the name is changed
#main.py from mysingleton import a as a1 print('a1',id(a1)) from mysingleton import a as a2 print('a2',id(a2)) ##########output########## a1 140212539471856 a2 140212539471856
Or there are multiple py scripts
#mysingleton.py class Animal(object): def run(self): print('running...') a = Animal()
A file called this variable a
#funcs.py from mysingleton import a def fun(): print('in fun a:',id(a))
Another file imports the fun
#main.py from mysingleton import a import funcs funcs.fun() print('in main a',id(a))
What will be the conclusion of the final operation?
##########output########## in fun a: 139630131360320 in main a 139630131360320
So an object imported from these two scripts is a singleton object. However, it should be noted that we import an object after instantiation. If we only import the class and then instantiate it in the script, it is not a singleton variable
#main.py from mysingleton import Animal a = Animal() b = Animal() print('a',id(a)) print('b',id(b))
In the above example, a and b are general instantiated objects.
Using the decorator
Just take a look at this. It is not the main knowledge point
def Singleton(cls): _instance = {} def _singleton(*args,**kwargs): if cls not in _instance: _instance[cls] = cls(*args,**kwargs) return _instance[cls] return _singleton @Singleton class A(object): def fun(self): pass a = A() b = A() print('a',id(a)) print('b',id(b))
Another way is to use metaclass metaclasses. However, some problems may occur in some multi-threaded environments. We won't talk about them here.