The Inheritance of Class in python3 and the Difference between self and super

  • 2021-07-01 07:57:02
  • OfStack

Inheritance of classes in python:

The child class inherits the parent class, and the child class owns the properties and methods of the parent class.

Classes in python are initialized to __init__ (). So both the parent class and the child class are initialized as __init__ (), but if the child class is initialized without this function, then it calls the parent class's __init__ (); If this function is implemented, it overrides the initialization function of the parent class. If you inherit __init__ () from the parent class, you need to show the call to this function in the subclass. The implementation is as follows:


class Animal(object):
 def __init__(self):
  self.name = " I am the parent class "
 
class Panda(Animal):
 def __init__(self):
  super().__init__() # Use super To display the method that calls the parent class __init__() Function 
 
if __name__=="__main__":
 panda = Panda() # Instantiation Panda
 print(panda.name)

 I am the parent class   # The output shows that the initialization function of the parent class is used and the name Attribute 

Subclasses can also define their own properties in initialization functions:


class Animal(object):
 def __init__(self):
  self.name = " I am the parent class "
 
class Panda(Animal):
 def __init__(self):
  super().__init__()
  self.myname = "panda"
 
if __name__=="__main__":
 panda = Panda()
 print(panda.myname)

panda  # Subclass's own attributes 

Differences between self and super:

★ ES 23EN is the first method to call itself if it does not go to the parent class to find it; super is to find methods directly from the parent class

★ self is a class and super is a precompiled instruction

★ The outputs of self class and super calss are one


class Animal(object):
 def __init__(self):
  self.name = " I am the parent class "
 
 def A(self):     # In the parent class A Method 
  print(" Of the parent class A Method ")
 
class Panda(Animal):
 def __init__(self):
  super().__init__()
  self.myname = "panda"
 
 def A(self):     # Subclass in the A Method 
  print(" Subclass of A Method ")
 
 def B(self):
  self.A()  #self Call A
  super().A()  #super Call A
 
 
if __name__=="__main__":
 panda = Panda()
 panda.B()   # Pass B Function to call the A Method to view self And super Difference between 

 Subclass of A Method    # We said self It is to find the method from itself first, instead of going to the parent class to find it 
 Of the parent class A Method    # And super Is found directly from the parent class 

If there is no A method in the subclass, it will output:


 Of the parent class A Method   # Subclass does not, self Find from the parent class 
 Of the parent class A Method 

If the parent class does not have an error, it will report an error

The above is the basic explanation of inheritance in python and the difference between self and super. In fact, the class is still a lot of complex places, in the use of the process will slowly learn, here is only an entry-level description.


Related articles: