Skip to content

Inheritance 继承 of a class in Python

Concept of inherit

The concept of inheritance is we based on an old Blueprints / Template to make changes to become a new Blueprints / Template.

For exmaple: We will inherit a old class and make changes to it to become a new class, instead of create a new class.

Although, create a new class can help us define a new template of phones. But it could make our code repeated. Choice of a new feature

Example of inherit a class

So the concept of inheritace of class, can help us write less code and when we inheritance a class, all of the properties / behaviour from a parent class will also inherit to a child class.

Syntax

py
class Parent:
    ...

class Child(Parent):
    ...

Usage

1. Single Inheritance

class-inherit.py

py
class Phone:
    name = None
    IMEI = None
    
    def call_on_4g(self):
        print(f"On call with 4g - {self.name}")
        
class NewPhone(Phone):
    
    def call_on_5g(self):
        print(f"On call with 5g - {self.name}")
        
p4g = Phone()
p4g.IMEI = 11111111111111
p4g.name = "Phone 10"
p4g.call_on_4g()

p5g = NewPhone()
p5g.IMEI = 22222222222222
p5g.name = "Phone 11"
p5g.call_on_4g()
p5g.call_on_5g()

Output:

On call with 4g - Phone 10
On call with 4g - Phone 11
On call with 5g - Phone 11

2. Multiple Inheritance

Multiple inheritance occurs when a child class inherits from more than one parent class.

Multiple Inheritance class

class-inherit-2.py

py
# Features of a Phone

class Camera:
    camera_lens_brand = None
    sensor_size = None
    
    def take_photo(self):
        print(f"{self.camera_lens_brand} is taking photo right now")
    
class ProccessingUnit:
    cpu_name = None
    cpu_speed = None
    gpu_name = None
    gpu_ram = None
    ram = None
    storage = None

    def get_spec(self):
        print("Spec of the phone")
        print(f"Ram available (GB): {self.ram}")
        print(f"Lenses Brand: {self.camera_lens_brand}" )
        print(f"Port Current Voltage: {self.port_current_voltage}v")
    
class IOPort:
    port_speed = None
    port_current_voltage = None
    
class Phone(Camera, ProccessingUnit, IOPort):
    pass
        
p = Phone()
p.ram = 12
p.camera_lens_brand = "Leica"
p.port_current_voltage = 5

p.get_spec()
p.take_photo()

Output:

Spec of the phone
Ram available (GB): 12
Lenses Brand: Leica
Port Current Voltage: 5v
Leica is taking photo right now

pass keywords

  • pass keywords means that the content of a class is empty.

Inheritance with replacing

class-inherit-with-replacing.py

py
class Phone:
    IMEI = None
    brand = None
    name = None
    chips_version = 1
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version}")
        
class Phone2(Phone):
    
    chips_version = 2
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version} [LATEST]")
        
    
    def call_with_5g(self):
        print(f"Run call on {self.name} - {self.brand} with 5g band and the chip version of {self.chips_version} [LATEST]")
        
p1 = Phone()
p1.IMEI = 78883875992783578
p1.brand = "Star"
p1.name = "S1"
p1.call_with_4g()

p2 = Phone2()
p2.name = "s2"
p2.brand = "Star"
p2.IMEI = 87445555699211120
p2.call_with_4g()
p2.call_with_5g()

INFO

  • The Parents attributes and behaviour can be changes by re-define on a Children class.

Output:

Run call on S1 - Star with 4g band and the chip version of 1
Run call on s2 - Star with 4g band and the chip version of 2 [LATEST]
Run call on s2 - Star with 5g band and the chip version of 2 [LATEST]

Getting the Parent original value after Inheritance with replacing

Method 1: Parent.attrs

class-inherit-with-replacing.py

py
class Phone:
    IMEI = None
    brand = None
    name = None
    chips_version = 1
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version}")
        
class Phone2(Phone):
    
    chips_version = 2
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version} [LATEST] [Last version {Phone.chips_version}]")
        
    
    def call_with_5g(self):
        print(f"Run call on {self.name} - {self.brand} with 5g band and the chip version of {self.chips_version} [LATEST]")
        
p1 = Phone()
p1.IMEI = 78883875992783578
p1.brand = "Star"
p1.name = "S1"
p1.call_with_4g()

p2 = Phone2()
p2.name = "s2"
p2.brand = "Star"
p2.IMEI = 87445555699211120
p2.call_with_4g()
p2.call_with_5g()

INFO

  • The Parents attributes and behaviour can be changes by re-define on a Children class.
  • After replacing the original the value after inheritance, The attributes and behaviour can obtain from: Parents.attr or super().attrs

Output:

Run call on S1 - Star with 4g band and the chip version of 1
Run call on s2 - Star with 4g band and the chip version of 2 [LATEST] [Last version 1]
Run call on s2 - Star with 5g band and the chip version of 2 [LATEST]

Method 2: super keywords

class-inherit-with-replacing.py

py
class Phone:
    IMEI = None
    brand = None
    name = None
    chips_version = 1
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version}")
        
class Phone2(Phone):
    
    chips_version = 2
    
    def call_with_4g(self):
        print(f"Run call on {self.name} - {self.brand} with 4g band and the chip version of {self.chips_version} [LATEST] [Last version {super().chips_version}]")
        
    
    def call_with_5g(self):
        print(f"Run call on {self.name} - {self.brand} with 5g band and the chip version of {self.chips_version} [LATEST]")
        
p1 = Phone()
p1.IMEI = 78883875992783578
p1.brand = "Star"
p1.name = "S1"
p1.call_with_4g()

p2 = Phone2()
p2.name = "s2"
p2.brand = "Star"
p2.IMEI = 87445555699211120
p2.call_with_4g()
p2.call_with_5g()

Output:

Run call on S1 - Star with 4g band and the chip version of 1
Run call on s2 - Star with 4g band and the chip version of 2 [LATEST] [Last version 1]
Run call on s2 - Star with 5g band and the chip version of 2 [LATEST]

Conclusion

  • The Parents attributes and behaviour can be changes by re-define on a Children class.
  • After replacing the original the value after inheritance, The attributes and behaviour can obtain from: Parents.attr or super().attrs

Class inheritance conclusion