Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / Python Tutorial / Chapter 5: Object-Oriented Programming (OOP)
PYTHON ⏱ 12 min read

OOP Fundamentals: Classes, Objects, and __init__

Object-Oriented Programming (OOP) is the programming paradigm used to model real-world concepts into structured software entities.

What is it?

A `class` is a blueprint for creating objects. An `object` is an instance of a class containing its own state (attributes) and behavior (methods).

Why is it used?

Organizes complex code into manageable, reusable modules. Underpins Flask models, Django ORM, and enterprise systems.

Detailed Explanation

Key OOP Concepts in Python: • The `class` keyword: Defines a blueprint. • The `__init__()` method: The constructor method automatically executed when a new object instance is created. • The `self` parameter: A reference to the current instance of the class. It allows methods to access and modify that specific object's attributes. • Instance Attributes: Bound to `self` (`self.name = name`). • Class Attributes: Shared across all instances of the class.

Syntax Structure

Syntax Blueprint
class ClassName:
    def __init__(self, param1):
        self.param1 = param1

    def method_name(self):
        return self.param1

Code Example

PYTHON Code
class Student:
    # Class Attribute: Shared across all students
    institute_name = "IGM Computer Institute"

    def __init__(self, student_id, name, course, balance=2500):
        # Instance Attributes: Unique to each student instance
        self.student_id = student_id
        self.name = name
        self.course = course
        self.balance = balance

    # Instance Method
    def make_payment(self, amount):
        if amount <= 0:
            print("Payment amount must be greater than zero.")
            return
        self.balance -= amount
        print(f"Payment of ₹{amount} received for {self.name}. Remaining dues: ₹{self.balance}")

    def get_details(self):
        return f"[{self.student_id}] {self.name} | Course: {self.course} | Due: ₹{self.balance}"

# Creating instances (objects)
student1 = Student(101, "Rahul Deshmukh", "Advanced Python")
student2 = Student(102, "Sneha Patil", "Data Analytics", balance=5397)

print(student1.get_details())
student1.make_payment(1500)
print(student1.get_details())
print(f"Institute: {student1.institute_name}")

Explanation of the Code

• `class Student` defines the blueprint. • `__init__` initializes each student with unique `name`, `course`, and `balance`. • `make_payment` mutates the specific student's internal balance.

Expected Output

[101] Rahul Deshmukh | Course: Advanced Python | Due: ₹2500 Payment of ₹1500 received for Rahul Deshmukh. Remaining dues: ₹1000 [101] Rahul Deshmukh | Course: Advanced Python | Due: ₹1000 Institute: IGM Computer Institute

Real-World Application

Every modern web framework models database rows as OOP classes (e.g. `User`, `Course`, `Order`).

Common Mistakes to Avoid

• Forgetting `self` as the first parameter in method definitions (throws `TypeError: takes 0 positional arguments but 1 was given`). • Confusing class attributes with instance attributes.

Quick Practice Challenge

Create a `Course` class with `title`, `duration`, and `price` attributes, and a `get_discounted_price(discount_pct)` method.

Key Takeaways

• A class is a blueprint; an object is an instance. • `__init__()` initializes the object's instance attributes. • `self` refers to the specific instance calling the method.