OOP in Python Beginner Notes
Classes, objects, inheritance, polymorphism, dunder methods, and properties.
OOP in Python — Detailed Notes
OOP in Python is an important chapter in Python Programming and is frequently tested in both conceptual and application-based questions. Students should first understand the core definition, then connect the topic with real-life observations and exam patterns.
Python OOP allows defining classes as blueprints for objects with attributes and methods, supporting inheritance, encapsulation, and polymorphism. In school and entrance exams, questions usually check your conceptual clarity, step-wise logic, and ability to avoid common mistakes.
To prepare effectively, break OOP in Python into smaller sub-parts: definition, laws/rules, examples, formulas, and revision questions. After theory, solve short questions, then move to mixed-level numericals or application prompts.
A smart revision strategy is to maintain a one-page summary for OOP in Python. Include important terms, two solved examples, and last-minute checkpoints before exams.
Key Exam Points
- `__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.
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?
Q2.Explain method resolution order (MRO) in Python's multiple inheritance.
Q3.What are `__str__` and `__repr__`? When are they called?
🔘 MCQ Practice — OOP in Python
MCQ 1.What does `@property` decorator do in Python?
✓ 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?
✓ 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.