Since the most recent project involves a singleton pattern several times, make a small note.
The teacher used to only a certain file in the entire project to customize a class, and then instantiate an object in the Py file, other files call this instantiation of the class object is called him singleton mode, raised the question
There is currently only one real singleton pattern:
Class Single_instance (object):
__instance=none
def __init__ (self):
Pass
Def __new__ (CLS, *args, **kwargs):
If Cls.__instance==none:
cls.__instance=object.__new__ (Cls,*args,**kwargs)
Return cls.__instance
A=single_instance ()
B=single_instance ()
Print (a)
Print (b)
The output results are as follows:
<__main__.singleton Object at 0x0000016d5191d320>
<__main__.singleton Object at 0x0000016d5191d320>
This is called a singleton mode, okay?!! A class regardless of how many times you instantiate, his object is always a memory address.
Python single-instance mode