What is a single instance of Python?
In Python, if the base class of the class we created is not intentionally specified, then all classes inherit from the ancestor of the type, and all of the classes we create are instances of the ancestor of type.
For example:
class A: def __init__ (self): " Hello world! " def __str__ (self): return = A ()print#A is an instance of a.
So when we do this,
b = A ()
c = A ()
..... , we create instances of multiple Class A, and then they (A,B,C) instances of these classes are independent, because we do not do a similar behavior like D =a (), will be written in memory of this code is stored in a different memory space, This shows that there is no association between the instances of these classes A, who cannot affect who,
We can verify the output through the following code
classA:def __init__(self): self.welcom="Hello world!" def __str__(self):returnrepr (self.welcom) a=A () b=A () C=A ()Print(ID (a))Print(ID (b))Print(ID (c)) ID (a) is used to output the address of the memory space where the instance is located, and you can see that the address of the memory space where they reside is different.
Then the concept of single-instance is obvious, that is, whenever I do something like this (A=a ()), I do not let the code I write into another address space in memory, but let them reference the same piece of code, it is equivalent to me to define a function
Def single ():
Print("youcan reference me! " ) = = = single print (ID (a)) print (ID (b) )print(ID (c)) when I print them in
In the memory address space, the result of the output is this, they instantiate the function when it does not copy the code into another memory space.
So how do you do such a single instantiation of a class when defining a class?
defSingleton (CLS): Instace= {} defgetinstance ():ifCls not inchInstace:instace[cls]=CLS ()returnInstace[cls]returngetinstanceclassCount:def __init__(self): Self.age= 1Self.name='Zhuwei' def __str__(self):return("name is:%s -is%s"%(Self.name,self.age)) Single=Singleton (Count) a=Single ()Print(a) b=Single ()Print(b)Print(A isb)Print(ID (a))Print(ID (b))Print(ID (b)) The use of closures (closures are nested in a function of one or more functions, and the nested function can access its parent function of the variable, the outer function accepts a class or function as a parameter, and finally return the nested function) method implementation of a single instance of the class,
defSingleton (CLS): Instace= {} defgetinstance ():ifCls not inchInstace:instace[cls]=CLS ()returnInstace[cls]returngetinstanceclassCount:def __init__(self): Self.age= 1Self.name='Zhuwei' def __str__(self):return("name is:%s -is%s"%(Self.name,self.age)) Single=Singleton (Count) a=Single ()Print(a) b=Single ()Print(b)Print(A isb)Print(ID (a))Print(ID (b))Print(ID (b))
The instances of these two classes are in the same address in the memory space
Using closures (closures are nested in a function of one or more functions, and the nested function can access the variables of its ancestor function, the outer function accepts a class or function as a parameter, and at the end returns nested functions) The method implements a single instance of the class,
You can also use the __new__ magic method in Python to implement a single instance of a class
classSingleton:instace=None#def __new__ (cls,name,base,dic): def __new__(cls,*args,**Kwargs):ifCls.instace isNone:cls.instace= Super (SINGLETON,CLS).__new__(cls,*args,**Kwargs)returnCls.instacedef __init__(self): Self.name='Zhuwei'a= SingleTON () b=SingleTON ()Print(ID (a))Print(ID (b))
Python Single Instance