used to create a single python __new__ embodiment mode (singletons)

# Singleton pattern: a class that the object is to produce only one. Their id addresses point to the same memory address

Step one: understand who created the object

# Singleton pattern 
# First understand that we are creating a class object, when, in fact, is the parent of this class call that inherited object ,
# then call the object of the new method to create an object.
# After you create an object, it will perform __init__ method, initialize


# The first step: 
class the Person:
     __instance = None 

    DEF  __new__ (CLS, * args, ** kwargs):
         Print ( " 1111 " ) 

    DEF  __init__ (Self):
         Print ( " After creating the object, initialized " ) 

the p- = the Person ()   # result 1111 
# from the results of the analysis: I've created this new method, will first perform my own new methods, but did not produce objects (because there is no __init__ method of execution) 
# when we create an object, is object For this class helped us produce an object. So we have to call the parent class (object) of the new method to produce an object.

The second step to understand the execution order of objects:

# Therefore: The following methods can help us to produce an object .

class the Person:
     __instance = None 

    DEF  __new__ (CLS, * args, ** kwargs):
         Print (111 ) 
        obj . = Object __new__ (CLS)   # pass this class we want to produce objects 
        return obj 

    DEF  __init__ (Self):
         Print ( " after creating the object, initialized " ) 


p1 = the Person ()
 # 111 
# after you create an object, initialized 

'' ' 
results analysis: 
I call __new__ method objcet help me have an object, and the object also performed __init__ method. 

'' '

The third step: to understand how to create an object only by what conditions to create only one object

# Understanding through case two above. I know I should be called the father of the new method to create an object, 
and I hope that this, create an object that object again when the object is created or the beginning of creation.
So I want to use private variables defined to be a judge, if the private variable __instance has a value I directly
return the object that created the beginning. If you did not create, then call the parent class new method to create an object.
class the Person:
     __instance = None 

    DEF  __new__ (CLS, * args, ** kwargs):
         Print ( " 1111 " )
         IF the Person. __instance  IS None: # only the first time, the condition is None, so the first time just create an object, the latter condition is not false, so the direct return of the object is first created 
            obj = object. __new__ (CLS)   # inherited object calls the new method produces an object 
            the Person. __instance = obj      # will be assigned to this object this class private variables __instance 

        return the Person. __instance 

    DEF  __init__ (Self):
        Print ( " After creating the object, initialized " ) 


p1 = the Person () 
P2 = the Person () 
p3 = the Person () 
P4 = the Person ()
 Print (the above mentioned id (p1), the above mentioned id (P2), the above mentioned id (p3), the above mentioned id ( P4)) 

output: show four objects are the same memory address
 416 664 513 336 416 664 513 336 416 664 513 336 416 664 513 336
 

 

 

 

Guess you like

Origin www.cnblogs.com/one-tom/p/11264501.html