before reading, learned that in Python can be implemented by the __new__ method of the singleton mode, code an example below, I have a few questions, what is a singleton mode? What is the __new__ method for? Does the singleton pattern implemented with the __new__ method, such as the following MyClass class, affect the initialization of the class? Does it affect instance methods, class methods, static methods of the class? The following will say my understanding of these concepts, if there is a mistake, welcome the Exchange point, thank you.
1 classSingleTon (object):2_instance = {}3 4 def __new__(CLS, *args, * *Kwargs):5 ifCls not inchcls._instance:6CLS._INSTANCE[CLS] = Super (SingleTon, CLS).__new__(CLS, *args, * *Kwargs)7 Printcls._instance8 returnCls._instance[cls]9 Ten One classMyClass (SingleTon): AClass_val = 22
First of all, the singleton mode is to ensure that there is only one instance of a class, and this instance is created by itself and is used in the system. Singleton mode is a design pattern, about the design pattern and the singleton mode more specific content, you can view the relevant books, in the back I also want to study these things.
The following is the main __new__ is used to do, in Python, __new__ is used to create an instance of a class, and __init__ is used to initialize the instance. Since __new__ is used to create instances, it is also necessary to return an instance of the corresponding class, so what happens if you return an instance of another class? See the code below. After the following code is run, print out the noreturn __new__ and print out the other instance, and finally through type (t) you can see that the type of T is <class ' __main_ _. Other ' >, you can know that if you do not return instances of this class in __new__, you cannot invoke the __init__ method. To return an instance of this class, simply change the other () of 12 lines in the following code to Super (Noreturn, CLS). __new__ (CLS, *args, * * Kwargs) .
1 classOther (object):2val = 1233 4 def __init__(self):5 Print 'Other instance'6 7 8 classNoreturn (object):9 Ten def __new__(CLS, *args, * *Kwargs): One Print 'Noreturn __new__' A returnOther () - - def __init__(Self, a): the Printa - Print 'Noreturn __init__' - -t = Noreturn (12) + PrintType (t)
Finally, the singleton pattern implemented by the __new__ method will affect the instance method, class method, static method, instance variable and class variable. The answer is that there is no effect on the corresponding method, but if the instance is initialized with a different variable and the instance variable and class variable are modified in the subsequent variables, the preceding one will be modified accordingly, and this is exactly the case, no matter how many variables point to this instance, they point to the same one. After the instance is generated in __new__, every time the instance object is initialized, the same instance is generated, and the related methods and variables in this instance are used just like normal instances. The test code and the output are as follows:
1 classMyClass (SingleTon):2Class_val = 223 4 def __init__(Self, val):5Self.val =Val6 7 defObj_fun (self):8 PrintSelf.val,'Obj_fun'9 Ten @staticmethod One defStatic_fun (): A Print 'Staticmethod' - - @classmethod the defClass_fun (CLS): - PrintCls.class_val,'Classmethod' - - + if __name__=='__main__': -A = MyClass (1) +b = MyClass (2) A PrintA isB#True at PrintID (a), id (b)#4367665424 4367665424 - #Type Validation - PrintType (a)#<class ' __main__. MyClass ' > - PrintType (b)#<class ' __main__. MyClass ' > - #instance Method -A.obj_fun ()#2 Obj_fun inB.obj_fun ()#2 Obj_fun - #class Method toMyclass.class_fun ()#Classmethod +A.class_fun ()#Classmethod -B.class_fun ()#Classmethod the #Static Methods *Myclass.static_fun ()#Staticmethod $A.static_fun ()#StaticmethodPanax NotoginsengB.static_fun ()#Staticmethod - #class Variables theA.class_val = 33 + PrintMyclass.class_val# A A PrintA.class_val# - the PrintB.class_val# - + #instance Variable - PrintB.val#2 $ PrintA.val#2
The difference between the __new__ and __init__ methods that are thought of by Python implementing singleton patterns through __new__