Python Object-Oriented Programming (OOP)

Ka Kavitha V Updated 03 Oct 2026
5 min read

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a way of organizing code by modeling real-world things as objects. Each object bundles together:

  • Data about the thing — called attributes or properties (for example, a student's name and marks)
  • Behavior of the thing — called methods (for example, calculating the student's average)

Python is an object-oriented language: it lets you build programs from classes and objects. In fact, you have been using objects all along — strings, lists, and dictionaries are objects, and methods such as "text".upper() and items.append(5) are their behaviors.

This lesson introduces the core ideas of OOP. The following lessons cover each part in detail.

Why Use OOP?

As programs grow, keeping related data and functions organized becomes harder. OOP helps by grouping them together. Its main benefits are:

  • A well-structured, organized codebase — related data and behavior live in one place.
  • Easier maintenance and debugging — changes to how something works are made in one class.
  • Code reusability — one class can be used to create many objects, and new classes can build on existing ones.
  • Less repetition, better readability — common logic is written once.
  • Scalability — large applications are easier to extend when they are divided into clear, independent pieces.

Procedural vs Object-Oriented Code

Without OOP, the data for a bank account and the functions that work on it are separate:

# Procedural style
account_owner = "Ravi"
account_balance = 1000

def deposit(balance, amount):
    return balance + amount

account_balance = deposit(account_balance, 500)
print(account_owner, account_balance)

Expected output:

Ravi 1500

With many accounts, you would need many separate variables, and nothing connects a balance to its owner.

With OOP, the data and behavior are bundled into one object:

# Object-oriented style
class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

account = BankAccount("Ravi", 1000)
account.deposit(500)
print(account.owner, account.balance)

Expected output:

Ravi 1500

Each BankAccount object keeps its own owner and balance, and knows how to deposit money into itself. Creating a second account is one line: BankAccount("Meera", 2000). Don't worry about every detail of this code yet — __init__() and self are explained in the next lessons.

The DRY Principle

DRY stands for Don't Repeat Yourself. It means avoiding duplicated code. Instead of rewriting the same logic in several places, put it in a function or class once and reuse it.

Duplicated code is risky: when the logic needs to change, every copy must be found and updated, and a missed copy becomes a bug. Classes support DRY by defining structure and behavior once for every object created from them.

Classes and Objects

Classes and objects are the foundation of OOP.

  • A class is a blueprint or template. It describes what data an object will have and what it can do.
  • An object is a concrete instance built from that blueprint, with its own actual data.

A Real-World Analogy

An architect's blueprint for a house is like a class. It specifies rooms, doors, and windows — but you cannot live in a blueprint. The houses built from it are like objects: each is real, each has its own address and paint color, and all of them follow the same design.

Class (Blueprint)Objects (Instances)
StudentAlice, Rahul, Meera
LaptopDell, HP, Lenovo
AnimalDog, Cat, Horse

Example: A Simple Class and Objects

class Student:
    pass

s1 = Student()
s2 = Student()

print(s1)
print(s2)

Example output (the memory addresses will differ on your computer):

<__main__.Student object at 0x7f8b2c3d4e50>
<__main__.Student object at 0x7f8b2c3d4f10>

Explanation:

  • class Student: defines a class named Student. The pass statement leaves the body empty for now.
  • Student() — the class name followed by parentheses — creates a new object. This is called instantiation.
  • s1 and s2 are two separate objects. The different addresses in the output show that they are distinct objects in memory.
  • __main__ is the name of the module where the class was defined (the script being run).

Each object is independent, but all objects follow the structure defined by their class.

Python's Built-in Types Are Classes Too

print(type("hello"))
print(type([1, 2, 3]))
print(type(Student()))

Expected output:

<class 'str'>
<class 'list'>
<class '__main__.Student'>

str and list are classes built into Python, and "hello" and [1, 2, 3] are objects (instances) of those classes. Your own classes work the same way.

How Objects Work

When an object is created from a class:

  • It gets its own attributes (properties) — the data defined by the class.
  • It can use all the methods — the functions defined inside the class.
class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(self.name, "says Woof!")

dog1 = Dog("Bruno")
dog2 = Dog("Coco")

dog1.bark()
dog2.bark()

Expected output:

Bruno says Woof!
Coco says Woof!

Both dogs share the same bark() behavior from the class, but each has its own name. This combination — shared behavior, individual data — is what makes objects powerful and reusable.

The Four Pillars of OOP

OOP is usually described in terms of four key principles. You will study each in later lessons:

PrincipleMeaning
EncapsulationBundling data and methods together, and controlling access to the data
InheritanceCreating a new class that reuses and extends an existing class
PolymorphismUsing one interface (such as a method name) for objects of different types
AbstractionExposing what an object does while hiding how it does it

What You Will Learn Next

In the upcoming lessons, you will explore:

  • Creating and using classes and objects
  • The __init__() method for initializing objects
  • The self parameter
  • Class properties (attributes) and methods
  • Inheritance and polymorphism
  • Encapsulation and inner classes

Common Mistakes

MistakeProblem
Forgetting the parentheses: s1 = Students1 refers to the class itself, not a new object
Confusing the class with its objectsThe class is the blueprint; each object holds its own data
Lowercase class namesWorks, but PEP 8 recommends PascalCase for classes: Student, BankAccount
Using OOP for very small scriptsSimple scripts are often clearer with plain functions
  • Python Functions — methods are functions that belong to a class
  • Python Classes and Objects — the next lesson
  • Python Dictionaries — another way to group related data, without behavior

0 Comments

Reviewed before they appear

No comments yet.

Python
Ask about this post
AI Ask about this post

Ask questions about Python Object-Oriented Programming (OOP) and get answers drawn from it.

Signed-in readers only.