Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Python 3 Fundamentals & OOP 20 mins read Free Lesson

Object-Oriented Programming in Python (Classes & __init__)

Define classes, the __init__ constructor, instance methods, self keyword, and class inheritance.

OOP in Python

class Course:
    def __init__(self, title: str, is_free: bool = True):
        self.title = title
        self.is_free = is_free
        self.students = []

    def enroll_student(self, student_name: str) -> None:
        self.students.append(student_name)
        print(f"{student_name} enrolled in {self.title}")

    def __str__(self) -> str:
        return f"Course({self.title}, Free={self.is_free})"