SoFunction
Updated on 2024-11-20

Single Inheritance and Multiple Inheritance Example Analysis in Python

This article example describes single inheritance and multiple inheritance in Python. Shared for your reference, as follows:

single inheritance

I. Introduction

Python also supports class inheritance; if a language doesn't support inheritance, classes don't make much sense. A derived class is defined as follows.

class DerivedClassName(BaseClassName1):
  <statement-1>
  .
  .
  .
  <statement-N>

You need to pay attention to the order of the base class in the parentheses, if there is the same method name in the base class and it is not specified when used in the subclass, python searches from left to right i.e., if the method is not found in the subclass, it searches from left to right to find out if the base class contains the method.

The BaseClassName (the base class name in the example) must be defined in the same scope as the derived class.
In addition to classes, expressions can also be used, which is useful when the base class is defined in another module:.

class DerivedClassName():

II. Codes

# -*- coding:utf-8 -*-
#! python3
class people:
  #Define basic attributes
  name =''
  age =0
  # Define private attributes, which cannot be accessed directly from outside the class.
  __weight =0
  #Define constructor methods
  def __init__(self,n,a,w):
     = n
     = a
    self.__weight = w
  def speak(self):
    print("%says: I'm %d years old."%(,))
    #Single Inheritance Example
class student(people):
  grade =''
  def __init__(self,n,a,w,g):
    # Call the constructor of the parent class in either of the following ways
    #people.__init__(self,n,a,w)
    super().__init__(n,a,w)
     = g
    # Override methods of the parent class
  def speak(self):
    print("%says: I'm %d years old and I'm in %d grade."%(,,))
s = student('ken',10,60,3)
()

III. Operational results

ken says: I'm 10 years old and I'm in 3rd grade.

multi-inheritance

I. Introduction

Python also has limited support for multiple inheritance. A class definition for multiple inheritance looks like the following example.

class DerivedClassName(Base1, Base2, Base3):
  <statement-1>
  .
  .
  .
  <statement-N>

You need to pay attention to the order of the parent class in the parentheses, if there is the same method name in the parent class and it is not specified when used in the subclass, python searches from left to right i.e., if the method is not found in the subclass, it will look for the parent class from left to right to find out if the parent class contains the method or not.

II. Codes

# -*- coding:utf-8 -*-
#! python3
# Class Definition
class people:
  #Define basic attributes
  name =''
  age =0
  # Define private attributes, which cannot be accessed directly from outside the class.
  __weight =0
  #Define constructor methods
  def __init__(self,n,a,w):
     = n
     = a
    self.__weight = w
  def speak(self):
    print("%says: I'm %d years old."%(,))
#Single Inheritance Example
class student(people):
  grade =''
  def __init__(self,n,a,w,g):
    # Call the constructor of the parent class
    people.__init__(self,n,a,w)
     = g
  # Override methods of the parent class
  def speak(self):
    print("%says: I'm %d years old and I'm in %d grade."%(,,))
# Another class, preparation before multiple inheritance
class speaker():
  topic =''
  name =''
  def __init__(self,n,t):
     = n
     = t
  def speak(self):
    print("My name is %s, I'm an orator, and the topic of my speech is %s."%(,))
#Multiple inheritance
class sample(speaker,student):
  a =''
  def __init__(self,n,a,w,g,t):
    student.__init__(self,n,a,w,g)
    speaker.__init__(self,n,t)
test = sample("Tim",25,80,4,"Python")
()# method name is the same, the default call is the method of the parent class that comes first in parentheses.

III. Operational results

My name is Tim, I'm an orator, and I'm speaking on the topic of Python.

Readers interested in more Python related content can check out this site's topic: thePython Object-Oriented Programming Introductory and Advanced Tutorials》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Summary of Python coding manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.