day19-Python operation and maintenance Development Foundation (__new__ magic method)

 

1. __new__ magic methods

 

 

# ### __new__ magic method 
'' ' 
    Trigger timing: instantiate an object of class generated when the trigger (trigger timing before __init__) 
    Function: Controls the creation of objects 
    parameters: at least one cls accept the current class, the other under the circumstances decided 
    return value: returns the object or generally None 
'' ' 

class MyClass2 (): 
    B = 2 
obj2 = MyClass2 () 

# (. 1) basic syntax 
"" " 
means __new__ magic methods of the parent class object, create an object 
need to pass cls this parameter represents the present type, oriented class is created object, returns 
"" " 
class MyClass (): 
    a =. 1
     DEF  __new__ is (cls):
         Print (cls) # <class '. in __main __ MyClass'> 
        # by parent object,__New__ created inside the magic method of the object 
        # 1 return to this subject
        return Object. __new__ is (CLS)
         # 2. returned objects of other classes 
        # return obj2 
        # 3. does not return any objects 
        # return None 
        
        
obj = MyClass ()    
 Print (obj)    
 # Returns This object 
# Print (obj.a) 
# Returns other class object 
# Print (obj.b) 

# (2) verify __new__ and two trigger time __init__ method 
"" " 
__new__ used to create objects 
__init__ to initialize the object is 
first created in the initialization 
__new__ fast trigger timing in __init__ 
"" " 

class MyClass ():
     DEF  __new__ (CLS):
         Print (1)
         Return Object. __New__ is (CLS) 

    DEF  the __init__ (Self):
         Print (2 )     
        
obj = MyClass () 

# pass a single argument 
class MyClass ():
     DEF  __new__ is (CLS, name):
         return Object. __New__ is (CLS) 

    DEF  the __init__ (Self, name): 
        the self.name = name 
        
obj = MyClass ( " Zhouyong Ling " ) 

# pass in the case where a plurality of parameters 
class MyClass ():
     #The plurality of parameters, with the collected parameters to ensure the one-parameter argument 
    DEF  __new__ is (CLS, args *, ** kwargs):
         return Object. __New__ is (CLS) 
        
    DEF  the __init__ (Self, name, Skin, Age): 
        Self .name = name 
        self.skin = Skin 
        self.age = Age 
obj = MyClass ( " Ling-Ling Chou " , " green " , 108 )
 Print (obj.name) 

# Note the point: 
"" " 
If this is not their own return __new__ object class, does not trigger constructor __init__ 
"" " 
class MyClass (): 

    DEF __new__ is (CLS, args *, ** kwargs):
         Print ( " __new__ is triggered " )
         return obj2 
        
    DEF  the __init__ (Self):
         Print ( " the __init__ constructor triggered " ) 
obj = MyClass ()
__new__ magic methods Sample Code
# ### single-state model: no matter how many times an object is instantiated, it has one and only one object 

# (1) The basic syntax 
"" " In order to save space, speed up the efficiency of the proposed single-state mode " "" 
class Singleton ():
     __obj = None
     DEF  __new__ (CLS):
         IF CLS. __obj  iS None: 
            . CLS __obj = object. __new__ (CLS)
         return CLS. __obj 

"" " 
the first instantiation, cls .__ obj is None returns True 
execution object. __new __ (cls) returns the object let cls .__ obj Object to accept 
return of the object 

second instantiation, cls .__ obj is None returns False 
return CLS space for object heap of memory returned .__ obj

The third instantiation, cls .__ obj is None returns False 
target return cls .__ obj heap memory space to store the return 

after .. each instantiated object, that object is first stored 
on the realization of regardless of several examples, returns the same object; 
"" " 

OBJ1 = the singleton () 
obj2 = the singleton () 
OBJ3 = the singleton ()
 Print (OBJ1, obj2, OBJ3)
 Print (OBJ1 IS obj2) 

# (2) singlet mode + constructor 
class the Singleton ():
     __obj = None
     DEF  __new__ is (CLS, args *, ** kwargs):
         IF CLS. __obj  IS None: 
            . CLS __obj. = Object __new__ is (CLS)
         return CLS. __Obj  

    DEF  the __init__ (Self, name): 
        the self.name = name 

OBJ1 = the Singleton ( " Selena " ) 
obj2 = the Singleton ( " Huang Lexi " )
 Print (obj1.name)
 Print (obj2. name) 

"" " 

the first instantiation, self.name = Selena 
first instantiation, returns the first instance of an object out of 
the former property of the object name changed from Selena Wong Lok West 
self .name = yellow Western music 

print obj1.name obj2.name are yellow Western music 

. "" "
Single-state mode Sample Code

 

2. 

 

 

 

 

 

day19

Guess you like

Origin www.cnblogs.com/reachos/p/12177422.html