Back to Python Programming
🐍Python Programming

OOP in Python

Classes, objects, inheritance, polymorphism, dunder methods, and properties.

BeginnerIntermediateAdvanced

What You Will Learn in OOP in Python

Python OOP allows defining classes as blueprints for objects with attributes and methods, supporting inheritance, encapsulation, and polymorphism.

  • `__init__` is Python's constructor; `self` refers to the current instance.
  • Name mangling: `__attr` makes attribute private (not truly, but harder to access externally).
  • Inheritance: `class Child(Parent)` — `super()` calls parent's methods.
  • Dunder (magic) methods: `__str__`, `__repr__`, `__len__`, `__add__` enable operator overloading.
  • @property decorator: define computed attributes accessed like attributes (not methods).
  • Multiple inheritance supported; MRO (Method Resolution Order) follows C3 linearisation.

Syntax

class ClassName(ParentClass):
    class_var = value          # class variable (shared)

    def __init__(self, args):
        self.instance_var = args  # instance variable

    def method(self):
        return self.instance_var

    def __str__(self):         # string representation
        return f"ClassName({self.instance_var})"

Complete Code Example

class BankAccount:
    interest_rate = 0.05   # class variable

    def __init__(self, owner, balance=0):
        self.owner = owner
        self.__balance = balance   # private

    @property
    def balance(self): return self.__balance

    def deposit(self, amount):
        if amount > 0: self.__balance += amount

    def __str__(self):
        return f"Account({self.owner}, ₹{self.__balance})"

class SavingsAccount(BankAccount):
    def add_interest(self):
        self.deposit(self.balance * BankAccount.interest_rate)

acc = SavingsAccount("Priya", 10000)
acc.add_interest()
print(acc)   # Account(Priya, ₹10500.0)

Example

A `Student` class with `__init__(name, roll)` creates student objects with unique attributes.

Expected Exam Questions — OOP in Python

Q1.What is `self` in Python? Is it mandatory?
Answer: `self` refers to the current instance of the class. It must be the first parameter of all instance methods. The name `self` is a convention (not a keyword) — you could use any name, but `self` is universally used.
Q2.Explain method resolution order (MRO) in Python's multiple inheritance.
Answer: MRO determines the order in which base classes are searched for a method. Python uses C3 linearisation. Check with `ClassName.__mro__`. Generally: class itself → left parent → right parent → common ancestors. The `super()` function follows MRO.
Q3.What are `__str__` and `__repr__`? When are they called?
Answer: `__str__` returns a human-readable string, called by `print()` and `str()`. `__repr__` returns an unambiguous developer-friendly representation, called by `repr()` and in the REPL. If `__str__` is missing, Python falls back to `__repr__`.

🔘 MCQ Practice — OOP in Python

MCQ 1.What does `@property` decorator do in Python?
A. Makes a method a class method
B. Allows a method to be accessed as an attribute
C. Makes an attribute private
D. Converts a method to a static method

✓ Correct Answer: Allows a method to be accessed as an attribute

MCQ 2.In Python, how do you call a parent class method from a child class?
A. parent.method()
B. base.method()
C. super().method()
D. this.method()

✓ Correct Answer: super().method()

Download OOP in Python PDF Notes

Get the complete OOP in Python notes as a PDF — free for enrolled students, or browse our public study materials library.

More Python Programming Topics

Related Subjects

Frequently Asked Questions — OOP in Python

What is OOP in Python in Python Programming?
Python OOP allows defining classes as blueprints for objects with attributes and methods, supporting inheritance, encapsulation, and polymorphism.
What is `self` in Python? Is it mandatory?
`self` refers to the current instance of the class. It must be the first parameter of all instance methods. The name `self` is a convention (not a keyword) — you could use any name, but `self` is universally used.
Explain method resolution order (MRO) in Python's multiple inheritance.
MRO determines the order in which base classes are searched for a method. Python uses C3 linearisation. Check with `ClassName.__mro__`. Generally: class itself → left parent → right parent → common ancestors. The `super()` function follows MRO.
What are `__str__` and `__repr__`? When are they called?
`__str__` returns a human-readable string, called by `print()` and `str()`. `__repr__` returns an unambiguous developer-friendly representation, called by `repr()` and in the REPL. If `__str__` is missing, Python falls back to `__repr__`.
How do I prepare OOP in Python for exams?
To master OOP in Python, start by reading the theory carefully, then go through solved examples step by step. Practice numericals (if applicable), revise key formulas, and attempt previous year questions. SII notes cover all these aspects in a structured manner.
Are these OOP in Python notes free?
Yes! SII provides free access to OOP in Python notes and introductory study materials. Enrolled students get full access to detailed notes, solved papers, and live doubt-clearing sessions.
Which exams ask questions from OOP in Python?
OOP in Python is an important topic tested in Beginner, Intermediate, Advanced board exams, as well as GATE (CS & IT), University Semester Exams. It frequently appears in both short-answer and long-answer sections.