Python OOP Inheritance Basics

Day 18 of my Python Full Stack journey. ✅ Today's topic: Inheritance — one of the most powerful concepts in OOP. Instead of writing the same code twice — you inherit it from a parent class. Here's what I typed today: # Parent class class Person: def __init__(self, name, age): self.name = name self.age = age def introduce(self): return f"Hi, I'm {self.name}, {self.age} years old." # Child class — inherits from Person class Student(Person): def __init__(self, name, age, course): super().__init__(name, age) # inherits parent __init__ self.course = course # Method overriding — redefining parent method def introduce(self): return f"Hi, I'm {self.name}. I study {self.course}." s1 = Student("Punith", 24, "Python Full Stack") print(s1.introduce()) # Hi, I'm Punith. I study Python Full Stack. 3 things that clicked today: → super() calls the parent class — no need to rewrite __init__ → Child class can override any parent method → Child class can also add brand new methods of its own Why this matters for Django: → Django's Model class is a parent class → When you write 'class Student(models.Model)' — you are inheriting from Django's Model → That one line gives your class database superpowers #PythonFullStack #Day18 #Inheritance #OOP #BuildingInPublic #100DaysOfCode #Bangalore

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories