๐ฅ Python Inheritance & Methods: PowerSlap Edition#
Note: I would highly recommend that nobody in the class participate in this crazy sport. Itโs dangerous and you could get hurt, however, it is somewhat curious what people will do for sport, fame, and money.
๐ Introduction#
Inheritance is a powerful feature in object-oriented programming that enables code reuse, better structure, and logical hierarchies. This chapter explores how inheritance works in Python, demonstrating its practical applications using a PowerSlap Fighters analogy and real-world coding examples.
By the end of this chapter, you will:
Understand the basics of inheritance and why itโs useful.
Learn how to extend classes using
super()
.Differentiate between instance methods and class methods.
๐ What is Inheritance?#
Inheritance allows us to create a base class (parent) and extend it with specialized child classes. This promotes code reuse and modularity.
Example: PowerSlap Fighters ๐ฅ#
In our scenario, we define a base class Fighter
, and then create specialized fighters with unique abilities:
Fighter
(Base Class)Slapper
(Child Class)HeavyHitter
(Child Class)
Class Hierarchy Diagram#
classDiagram
Fighter <|-- Slapper
Fighter <|-- HeavyHitter
class Fighter{
+name
+power
+durability
+introduce()
}
class Slapper{
+accuracy
+slap()
}
class HeavyHitter{
+stamina
+knockout_slap()
}
This structure ensures code reusability while allowing each fighter to have unique attributes and behaviors.
๐ฅ Creating a Base Fighter Class#
The base class Fighter
defines common properties for all fighters.
class Fighter:
def __init__(self, name, power, durability):
self.name = name # Fighter's name
self.power = power # Slap power level
self.durability = durability # Ability to withstand damage
def introduce(self):
"""Introduce the fighter."""
return f"๐ {self.name} enters the PowerSlap arena!"
Key Features:#
init()
initializes the fighter attributes.introduce()
provides a basic introduction method.
๐ฅ Creating Specialized Fighters with Inheritance#
We extend Fighter
to create two specialized fighter types: Slapper
and HeavyHitter
.
The Slapper
Class#
class Slapper(Fighter):
def __init__(self, name, power, durability, accuracy):
super().__init__(name, power, durability) # Inherit from Fighter
self.accuracy = accuracy # Additional attribute
def slap(self):
return f"๐๏ธ {self.name} delivers a {self.power} power slap with {self.accuracy}% accuracy!"
The HeavyHitter
Class#
class HeavyHitter(Fighter):
def __init__(self, name, power, durability, stamina):
super().__init__(name, power, durability) # Inherit from Fighter
self.stamina = stamina # Additional attribute
def knockout_slap(self):
return f"๐ฅ {self.name} goes for a brutal {self.power} power KO slap with {self.stamina}% stamina left!"
Key Features:#
super().init()
calls the parent class constructor to avoid redundant code.Each subclass inherits attributes and methods from
Fighter
while adding new ones.
โก Using Our Fighter Classes#
Letโs create and test our fighter classes:
slap_fighter = Slapper("John 'Quick Hand' Doe", 80, 60, 90)
heavy_fighter = HeavyHitter("Mike 'Iron Palm' Tyson", 100, 90, 70)
print(slap_fighter.introduce()) # ๐ John 'Quick Hand' Doe enters the PowerSlap arena!
print(
slap_fighter.slap()
) # ๐๏ธ John 'Quick Hand' Doe delivers an 80 power slap with 90% accuracy!
print(
heavy_fighter.introduce()
) # ๐ Mike 'Iron Palm' Tyson enters the PowerSlap arena!
print(
heavy_fighter.knockout_slap()
) # ๐ฅ Mike 'Iron Palm' Tyson goes for a brutal 100 power KO slap with 70% stamina left!
๐ John 'Quick Hand' Doe enters the PowerSlap arena!
๐๏ธ John 'Quick Hand' Doe delivers a 80 power slap with 90% accuracy!
๐ Mike 'Iron Palm' Tyson enters the PowerSlap arena!
๐ฅ Mike 'Iron Palm' Tyson goes for a brutal 100 power KO slap with 70% stamina left!
๐ Applying Inheritance Beyond Fighters#
Inheritance is widely used in various real-world applications, including graphing systems and optimization problems.
Example: Graphing System with Inheritance#
We can create a base Graph
class and extend it to support different graph types.
import matplotlib.pyplot as plt
class Graph:
def __init__(self, title):
self.title = title
def show_title(self):
return f"๐ Graph: {self.title}"
def show_caption(self, caption):
return f"๐ {caption}"
class LineGraph(Graph):
def __init__(self, title, x_values, y_values):
super().__init__(title)
self.x_values = x_values
self.y_values = y_values
def plot(self):
plt.plot(self.x_values, self.y_values)
plt.title(self.title)
plt.show()
Using Our Graph Classes#
line_chart = LineGraph("Stock Prices", [1, 2, 3, 4], [100, 150, 200, 250])
print(line_chart.show_title()) # ๐ Graph: Stock Prices
line_chart.plot() # Displays a line chart
print(line_chart.show_caption("This is my caption"))
๐ Graph: Stock Prices

๐ This is my caption
๐ Key Takeaways#
โ
Inheritance allows us to create a base class (Fighter
) and specialized versions (Slapper
, HeavyHitter
).
โ
super()
calls the parent constructor, keeping code clean and reusable.
โ Child classes can have additional attributes & methods beyond the parent class.
โ Inheritance is widely used in graphing, optimization, and structured modeling.
๐ฅ PowerSlap fighters inherit raw powerโjust like Python classes inherit code!