python class __init __, __ new __, __ class__ use Comments

1, python in all classes inherit the default object class, and object classes provide a lot of the original built-in properties and methods, all user-defined classes will inherit these properties built in python. We can () be viewed by dir. Although the python provides many built-in properties, but the actual development of commonly used much. The system provides built-in attributes much of the actual development of the user will have to use after rewriting.

class Foo (object): # in python3 the object can not write 
    Pass 
dir (Foo) # View provides built-in attributes to objects in python 
*** result **** 
[ ' __class__ ' , ' __delattr__ ' , ' __dict__ ' , ' __DIR__ ' , ' the __doc__ ' , ' __e 
at__ ' , ' __ge__ ' , ' __getattribute__ ' , ' __gt__ ' , '__hash__', '__init_
ubclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
 '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__
shook__', '__weakref__']

2, __ use of init__ and __new__

1, using the methods and functions __init__ :
     1 , used to construct the initialization function is used to initialize the instance of the class properties, the return value is not required.
    2 , the system automatically creates an instance when the call
     3 , if you do not define a custom class, then call the parent class's default, empathy is inherited, the subclass if not, call the parent, if so, call your own. 
   Article 3 For as exemplified: 
    class A ():
     DEF  the __init__ (Self, name): 
       the self.name = name
     class B (A):
         Pass 
   obj = B () # of instantiated B 
*** error **** 
 Traceback (MOST Last Call recent Results): 
  File " <stdin> " , Line 1, in <Module> 
TypeError: __init__(). 1 Missing required Positional argument: ' name 
    when we instantiate the B, B does not have __init__ method, but there is the parent class, this time we direct B () Examples will be given by obj =, we We need to pass parameters to the class B = obj ( ' Alex ' ) to instantiate
2, __new__ is method and functions :
    
     . 1, __new__ is a method for creating an object to a class , and return the object.
    2 , as to the class to create an instance, it is passed at least one parameter cls, parameter cls represents the instance of the class, this parameter is automatically provided by the Python interpreter when instantiating
     3 , the class is instantiated is a function to create an instance of the interior, and returns this example, therefore he is in the first instance method invocation is generally not considered to define the method
     4 , to create an instance returns an instance, so there must be a return value, return an instance of the parent class __new__ out, or is the object of direct _ _new__ out instances 

class Student (Object): # Object can not write in python 3 in 
    DEF  __init__ (Self, name): 
        self.name = name
         Print ( " this is the __init__ method " )
     DEF  __new__ (CLS, * args, ** kwargs):
         Print( ' This method is __new_ ' )    
         return obj. __New__ (CLS) 
S = Student ( ' Tom ' )
 **** ***** results were as follows: Note that the execution order before __new__ __init__ 
this method is __new__ 
this method is _init__
3. the __init__ use and contact __new__ 
  1. the __init__ first parameter is a Self , expressed the need for an initial example, automatically by the incoming python interpreter, and this example is an example of the return __new__
   2. Then __init__ __new__ on the basis of complete other initialization operation 
 
class Student (Object):
     DEF  __init__ (Self, name): 
        the self.name = name
         Print ( " it is __init__ method " ) 
 
    DEF  __new__ (CLS , * args, ** kwargs):
         Print ( " it's __new__ method " ) 
        the above mentioned id = Object. __new__ (CLS)
         Print (the above mentioned id) #Print this instance __new__ creates and returns the address in memory of the 
        return ID 
S1 = Student ( " JACK " )
 Print (S1)
 '' ' 
which is a method __new__ 
<__ main __. Student object at 0x000001EC6C8C8748> 
This is __init_ _ method 
<. __ main __ Student Object AT 0x000001EC6C8C8748> 
'' '  
summary: obviously, the same memory address both instances, so __init__ acceptable example is __new__ created.

3, __ class__ of use:

Pointing instance call __class__ properties of the corresponding class instance, so as _class__ function and type () function, where the classes are view objects

class Student(object):
    def __init__(self,name):
        self.name = name
stu = Student("tom")
print(type(stu),type(Student))
print(stu.__class__, Student.__class__, stu.__class__.__class__)
'''结果如下:
<class '__main__.Student'> <class 'type'>
<class '__main__.Student'> <class 'type'> <class 'type'>

4, the use of __class__ can find the object classes, see a face questions:

class A ():
     DEF show (Self):
         Print ( ' Base show ' )
 class B (A):
     DEF show (Self):
         Print ( ' DIC show ' ) 
how to call A's show method: 
obj = B () 
obj the .Show () # of instances of class B, class B because there show method, it directly obj.show () call is itself a method 
obj
 **** result ***** 
DIC show
 < __main__ . Object AT 0x00000000027EA9E8 B> # obj is an instance of the object class B 
answers: 
 . obj the __class__ = a #__class__ method points to the class object, only to give him an assignment type A, then call the method Show 
obj.show () 
obj
 **** result ***** 
Base Show
 < __main__ II.A, Object AT 0x00000000027EA9E8> # This is the time obj examples of object class a 
# ### note, as you change the class of the object obj, modify back after use through the obj .__ class __ = B, after the call to avoid the wrong way

 

Guess you like

Origin www.cnblogs.com/dushangguzhousuoli/p/11024067.html