Cheap VPS host selection
Provide server host evaluation information

Introduction to attributes and methods of python inheritance parent class

Python inheritance is a multi inheritance mechanism. A subclass can have multiple direct parents at the same time; Inheritance can get the methods defined by the parent class, and the child class can reuse the methods of the parent class.

1、 Inherited syntax

Subclass: the class that implements inheritance.

Parent class (base class, superclass): the inherited class.

When defining a subclass, a subclass inherits a parent class by placing multiple parent classes in parentheses after the subclass. If no direct parent class of this class is specified when defining a class, it inherits the object class by default, so the object class is the parent class of all classes (direct parent class or indirect parent class).

The syntax format is as follows:

class SubClass (SuperClassl , SuperClass2 , … ):
#Class definition part

Example:

class Animal:
def dog(self):
Print ('I have a dog! His name is% s'% self.dog_name)

class Child:
def son(self):
Print ('I have two sons!')

#Define Myself class, inherit Animal class and Child class
class Myself(Animal,Child):
def name(self):
Print ('My name is Xiaoming! ')

M=Myself() # Create Myself object
M. Dog_name='rhubarb' # Add dog_name class variable through Myself object
M. Dog () # Call the dog () method of Myself object to print that I have a dog! His name is rhubarb
M. Son() # Call the son() method of Myself object to print that I have two sons!
M. Name() # Call the name() method of Myself object to print my name as Xiao Ming!

In the above example, two parent classes, Animal and Child, and a Myself class that inherits Animal and Child classes are defined; After the Myself object is created, you can access the dog () method and the son () method of the Myself object, which indicates that the Myself object also has the dog () method and the son () method, so inheritance can get the methods defined by the parent class, and through inheritance of the child class, you can reuse the methods of the parent class.

2、 Multiinheritance

The subclass will inherit all the methods of the parent class. If multiple parent classes have the same method name, the method with the same name of the parent class in the front row will "mask" the method with the same name of the parent class in the back row. For example:

class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')

class Child:
def name(self):
Print ('I have two sons!')

class Myself(Animal,Child):
pass

class Yourself(Child,Animal):
pass

M = Myself()
M. Name() # Print that I have a dog named Dahuang!
Y = Yourself()
Y. Name() # Print that I have two sons!

In the above example, the Myself class inherits the Animal class and the Child class. When the Myself subclass object calls the name() method, because the name() method is not defined in the Myself subclass, Python will first search for the name() method in the Animal parent class, and once it is found, it will stop searching downward, so it runs the name() method in the Animal class; In Yourself subclass, because Child parent class is in the front row, the name() method of Child class is run.

3、 Rewrite

A subclass containing methods with the same name as the parent class is called method override (method override), which can be said to override the parent class method, or it can be said that a subclass overrides the parent class method.

Example:

class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')

class Myself(Animal):
#Override the name() method of the Animal class
def name(self):
Print ('I don't have a dog, I only have a cat, his name is Dabai!')

M=Myself() # Create Myself object
M. Name() # Execute the name() method of Myself object to print that I don't have a dog, I only have a cat, and his name is Da Bai!

In the above example, when running M.name(), it is no longer the fly() method of the Animal class, but the name() method of the Myself class.

After the method is rewritten in the subclass, if the overridden instance method in the parent class needs to be used, you can call the overridden method of the parent class by calling the instance method through the class name.

Example:

class Animal:
def name(self):
Print ('I have a dog, his name is rhubarb!')

class Myself(Animal):
#Override the name() method of the Animal class
def name(self):
Print ('I have a cat, his name is Dabai!')

def pet(self):
Animal. name (self) # Call the overridden parent class method name(), using the class name. instance name. You need to manually pass self
Self. name() # Execute the name() method and call the name() method overridden by the subclass

M=Myself() # Create Myself object
M.pet()
”’
Print
I have a dog, his name is rhubarb!
I also have a cat, his name is Dabai!
”’

4、 The super function calls the parent class constructor

If a subclass has more than one direct parent class, then the constructor in the front row will mask the constructor in the back row.

Example:

class Animal:
def __init__(self,pet,name):
self.pet = pet
self.name = name

def favourite_animal(self):
Print ('I have a% s, its name is% s!'% (self.pet,self.name))

class Fruit:
def __init__(self,kind):
self.kind = kind

def favourite_fruit(self):
Print ('My favorite fruit is% s!'% self.kind)

class Myself(Animal,Fruit):
pass

M=Myself ('dog ',' rhubarb ') # Create Myself object
M. Favorite_animal() # Call the favorite_animal() method of the Myself object to print that I have a dog named rhubarb!
M. Favorite_fruit() # Call the favorite_fruit method of Myself object. Because the kind instance variable of Fruit object is not initialized, an error is reported AttributeError: 'Myself' object has no attribute 'kind'

In the above example, the Myself subclass inherits the Animal parent class and the Fruit parent class. Since the Animal parent class is in front of the Fruit parent class, the constructor of the Animal parent class masks the constructor of the Fruit parent class. There is no problem running M.favorite_animal(). When running M.favorite_fruit(), the program will report an error because the kind instance variable of the Fruit object is not initialized.

To solve the above problem, Myself should rewrite the constructor of the parent class. The constructor of the child class can call the constructor of the parent class in the following two ways:

1. Use unbound methods, that is, parent class name__ Init__ (self, parameter 1, parameter 2....).

2. Use the super() function to call the parent class constructor.

First, check the help information of the super() function,

>>> help(super)
Help on class super in module builtins:

class super(object)
| super() -> same as super(__class__, <first argument>)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super().meth(arg)
| This works for class methods too:
| class C(B):
| @classmethod
| def cmeth(cls, arg):
| super().cmeth(arg)

Through the help information, you can see that when calling the instance method of the parent class, the first parameter self will be bound automatically; When calling class methods, the first parameter cls will be bound automatically.

Next, modify the above program:

class Animal:
def __init__(self,pet,name):
self.pet = pet
self.name = name

def favourite_animal(self):
Print ('I have a% s, its name is% s!'% (self.pet,self.name))

class Fruit:
def __init__(self,kind):
self.kind = kind

def favourite_fruit(self):
Print ('My favorite fruit is% s!'% self.kind)

class Myself(Animal,Fruit):
def __init__(self,pet,name,kind):
Fruit. __init__ (self, kind) # Call parent class construction method through unbound method
super().__ Init__ (pet, name) # Call the parent class construction method through the super() function

M=Myself ('Dog ',' Rhubarb ',' Apple ')
M. Favorite_animal() # Print that I have a dog named rhubarb!
M. Favorite_fruit() # Print that my favorite fruit is an apple!

Do not reprint without permission: Cheap VPS evaluation » Introduction to attributes and methods of python inheritance parent class