Python Classes and Objects

October 7, 2025

Object-Oriented Programming (OOP) is one of the most powerful and widely used programming paradigms.
In Python, OOP revolves around two key concepts — classes and objects.

If you’ve ever wondered how big applications are built using clean, reusable, and organized code — OOP is the answer.

In this blog, we’ll explore:

  • What is OOP?
  • What are Classes and Objects?
  • How to Create and Use a Class
  • The __init__() Constructor
  • Class and Instance Variables
  • Methods in Classes
  • Understanding self
  • Example: Building a Real-Life Python Class

🧠 1. What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a method of structuring a program by bundling related data and functions into objects.

Instead of writing scattered code and repeating logic, you can create classes that act as blueprints for reusable objects.

Example in real life:

  • A class is like a blueprint for a "Car".
  • An object is a specific car — like a Tesla Model 3 or BMW X5 — built using that blueprint.

🏗️ 2. What is a Class?

A class is a blueprint for creating objects.
It defines the properties (variables) and behaviors (methods) that an object will have.

Example:

class Car:
    # Class attributes (data)
    wheels = 4

    # Method (behavior)
    def start_engine(self):
        print("The engine has started.")

Here,

  • Car is the class.
  • wheels is a class variable (common to all cars).
  • start_engine() is a method (a function inside a class).

🚗 3. What is an Object?

An object is an instance of a class — it’s like creating a copy of the blueprint with real data.

# Create object of class Car
my_car = Car()

# Access class attribute
print(my_car.wheels)    # Output: 4

# Call class method
my_car.start_engine()   # Output: The engine has started.

You can create multiple objects from the same class, each representing a unique entity.

car1 = Car()
car2 = Car()

print(car1.wheels)  # 4
print(car2.wheels)  # 4


⚙️ 4. The __init__() Constructor

The __init__() method is a special method that automatically runs when a new object is created.

It’s used to initialize instance variables (unique data for each object).

Example:

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        print(f"Car: {self.brand} {self.model}")

Now, when we create objects:

car1 = Car("Tesla", "Model 3")
car2 = Car("BMW", "X5")

car1.display_info()   # Output: Car: Tesla Model 3
car2.display_info()   # Output: Car: BMW X5

Here:

  • self refers to the current object.
  • brand and model are instance variables unique to each object.

🧩 5. Understanding self

The keyword self is used inside a class to refer to the current object.

Whenever you access variables or methods inside a class, you must prefix them with self.

Example:

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def greet(self):
        print(f"Hello, my name is {self.name} and I’m in grade {self.grade}.")

s1 = Student("Aditi", 10)
s2 = Student("Ravi", 12)

s1.greet()  # Hello, my name is Aditi and I’m in grade 10.
s2.greet()  # Hello, my name is Ravi and I’m in grade 12.


🧱 6. Class Variables vs Instance Variables

TypeDefinedSharedAccessed using
Class VariableInside class, outside methodsShared by all objectsClassName.variable or self.variable
Instance VariableInside __init__()Unique for each objectself.variable

Example:

class Employee:
    company = "TechCorp"  # Class variable

    def __init__(self, name, salary):
        self.name = name      # Instance variable
        self.salary = salary  # Instance variable

e1 = Employee("John", 50000)
e2 = Employee("Sarah", 60000)

print(e1.company)  # TechCorp
print(e2.company)  # TechCorp

e2.company = "FinSoft"  # Overrides only for e2
print(e2.company)  # FinSoft
print(e1.company)  # TechCorp


🔁 7. Adding Methods to a Class

You can define multiple methods in a class to perform operations using instance data.

class BankAccount:
    def __init__(self, holder, balance=0):
        self.holder = holder
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"{amount} deposited. New balance: {self.balance}")

    def withdraw(self, amount):
        if self.balance >= amount:
            self.balance -= amount
            print(f"{amount} withdrawn. Remaining balance: {self.balance}")
        else:
            print("Insufficient balance!")

acc1 = BankAccount("Amit", 1000)
acc1.deposit(500)
acc1.withdraw(300)
acc1.withdraw(2000)

Output:

500 deposited. New balance: 1500
300 withdrawn. Remaining balance: 1200
Insufficient balance!


🧰 8. Example: Real-Life Application — Student Management

Let’s build a small example combining everything:

class Student:
    school_name = "Green Valley High"  # Class variable

    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def display(self):
        print(f"Student: {self.name}, Grade: {self.grade}")

    def promote(self):
        self.grade += 1
        print(f"{self.name} has been promoted to Grade {self.grade}.")

s1 = Student("Riya", 8)
s2 = Student("Arjun", 9)

s1.display()
s2.display()

s1.promote()

Output:

Student: Riya, Grade: 8
Student: Arjun, Grade: 9
Riya has been promoted to Grade 9.


🧭 9. Key OOP Concepts (At a Glance)

ConceptDescriptionExample
ClassBlueprint for creating objectsclass Car:
ObjectInstance of a classmy_car = Car()
Constructor (__init__)Initializes object variablesdef __init__(self):
MethodFunction inside classdef start(self):
selfRefers to current instanceself.name
Instance VariableUnique to each objectself.name
Class VariableShared by all objectsCar.wheels

💡 10. Why Use Classes and Objects?

Reusability — Create once, reuse multiple times.
Organized Code — Group related data and behavior together.
Scalability — Easier to extend and maintain.
Abstraction — Hides unnecessary details from the user.


🧾 Summary

In this blog, you learned:

  • What are classes and objects in Python
  • How to define and instantiate them
  • The use of __init__(), self, and methods
  • Difference between class variables and instance variables
  • Real-world examples using OOP

Classes and objects form the foundation of modern Python programming, especially in frameworks like Django, Flask, and data science libraries like Pandas.

© Copyright 2025 Fessorpro

Terms of Service / Privacy Policy