The concept of inheritance (literally, think of inheritance as in family assets) In Python, inheritance is a core concept of object-oriented programming, allowing a class (called a subclass) to inherit properties and methods from another class (called a superclass). This mechanism promotes code reuse, enhances code maintainability and scalability. Through inheritance, a subclass automatically acquires all properties and methods of the superclass. The subclass can also add new properties and methods or override methods in the superclass to provide specific implementations. (It’s somewhat like inheriting skills from a parent while also adding your own skills).Benefits of Inheritance
- Code Reuse: Subclasses can reuse the code of the superclass, avoiding code duplication. (Otherwise, it would be too much to write)
- Scalability: Subclasses can add new functionalities based on the superclass. (Upgrading on the original foundation)
- Maintainability: When code in the superclass needs modification, all subclasses inheriting from that superclass will automatically inherit these changes. (Changing one place affects the whole system)
Syntax of Inheritance
- Inheritance can be single or multiple. (Inheriting from a parent is single inheritance; if your child inherits from you, that is multiple inheritance).
- Inheritance is implemented by specifying the superclass when defining a class. The subclass name is followed by parentheses containing the name of the superclass.
Example of Single Inheritance:
# First, define two classes class A(object): # Define the first class to be the parent The superclass object is the base of all classes def __init__(self): # Initialization self.num = 1 # Leaving a piece of wealth def da(self): # Withdrawal command print(self.num) # Bank receipt
class B(A): # Define the second class to be the child The subclass inherits from the parent class (whoever is in the parentheses is inherited from, laying the groundwork for multiple inheritance) pass # The second generation does nothing, using pass to occupy the position
r = B() # Instantiate the object
r.da() # Use the wealth of the parent class Output: 1 Money withdrawn, send a receipt # Is it clear? This is single inheritance
Multiple Inheritance Example:
class A(object): def __init__(self): self.num = 1 # Set instance property num=1 when initializing class A def TesA(self): print(f'I am the wealth of my grandfather: {self.num}')class B(object): def __init__(self): self.num = 5 # Set instance property num=5 when initializing class B def TesB(self): print(f'I am the wealth of my father: {self.num}')
# Grandson appears
class C(A,B): # Inherit all family assets def __init__(self): super().__init__() # super() rewrites, first calls super().__init__() to initialize A self.a_num = self.num # Save A's num value to new property a_num B.__init__(self) # Call B's __init__, at this point self.num will be overwritten to 5 self.b_num = self.num # Save B's num value to new property b_num self.num = 3 # C's own num property # Override TesA method to access saved a_num def TesA(self): print(f'I am the wealth of my grandfather: {self.a_num}') # Override TesB method to access saved b_num def TesB(self): print(f'I am the wealth of my father: {self.b_num}') def TesC(self): print(f'I am my own wealth: {self.num}')
C = C() # Instantiate C.TesC() # Output: I am my own wealth: 3C.TesB() # Output: I am the wealth of my father: 5C.TesA() # Output: I am the wealth of my grandfather: 1
super() Rewrite Overriding: When a subclass rewrites a method with the same name as the superclass, it is called overriding. super() Rewrite: If the subclass and superclass methods are the same, the superclass method can be called using super().That’s all for the pre-holiday sharing. After the holiday, I will integrate it, and combining it with previous content should form a basic framework. Just focus on mastering functions and learning as needed. Wishing everyone a happy double holiday. Here is a review of the previous content: Python Operations Series – Understanding Object-Oriented Programming Python Operations Series – Understanding Module and Package Imports Python Operations Series – Understanding File Read and Write Python Functions Series – Higher-Order Functions Python Functions Series – Functions Python Loops Series – Loops Python Conditionals Series (Part 2) – if Statements Python Conditionals Series (Part 1) – Operators – Conditional Statements Python Basics Series (Part 8) – Tuples and Sets Python Basics Series (Part 7) – Dictionaries and Dictionary Operations Python Basics Series (Part 6) – Data Indexing and List Operations Python Basics Series (Part 5) – Strings Python Basics Series (Part 4) – input Function and Formatted Output Python Basics Series (Part 3) – Data Type Conversion Python Basics Series (Part 2) – Variables and Data Types Python Basics Series (Part 1)