Learn the core ideas behind Object‑Oriented Programming and Functional Programming, compare their pros and cons, and see easy Python examples to choose the right paradigm.
Programming paradigms are like philosophies that shape how you solve problems. Knowing more than one gives you extra tools, much like learning both a brush and a spray can if you are a painter. Two of the most influential are Object‑Oriented Programming (OOP) and Functional Programming (FP).
| Paradigm | One‑Sentence Summary |
|---|---|
| OOP | Model the world as objects that hold state (data) and behavior (methods). |
| FP | Build programs by composing stateless functions that transform data. |
| Concept | OOP Term | FP Term |
|---|---|---|
| Building Block | Object (instance of a class) | Pure Function |
| State Handling | Mutable fields inside objects | Immutable data |
| Behaviour | Methods | Function composition |
| Code Re‑use | Inheritance / Interfaces | Higher‑order functions / Closures |
| Flow Control | Imperative (step by step) | Declarative (what, not how) |
1class BankAccount:
2 def __init__(self, owner, balance = 0.0):
3 self.owner = owner
4 self._balance = balance # underscore hints “private”
5
6 def deposit(self, amount):
7 self._balance += amount
8
9 def withdraw(self, amount):
10 if amount > self._balance:
11 raise ValueError("Insufficient funds")
12 self._balance -= amount
13
14 def __str__(self):
15 return f"{self.owner}'s balance is: ${self._balance:,.2f}"
16
17# Usage
18acc = BankAccount("Alex", 100)
19acc.deposit(50)
20print(acc) # Alex's balance: $150.00
BankAccount class__init__(…)** — initiates a new account**deposit(…)** — adds money**withdraw(…)** — removes money safely**__str__(…)** — neat print‑out**In short, this class models a simple bank account: you can create it with an owner and opening balance, deposit money, withdraw money with built‑in safety checks, and print the current balance in a human readable format.
1# Pure function → never changes anything outside itself
2def add_tax(price, tax_rate=0.1):
3 """Return price including a 10 % sales tax by default."""
4 return price * (1 + tax_rate)
1# 1. Define class Greeter
2class Greeter:
3 def __init__(self, greeting):
4 self.greeting = greeting
5
6 def greet(self, name):
7 return f"{self.greeting}, {name}!"
8
9# 2. Create an instance of Greeter class (initialised with a greeting message)
10hello = Greeter("Hello")
11
12# 3. Use it
13print(hello.greet("Ved"))
1# 1. make_greeter returns a NEW function
2def greet(greeting, name):
3 return f"{greeting}, {name}!"
4
5# 2. Use it
6print(greet("Hello", "Ved"))
Both snippets output “Hello, Ved!” but reflect different mind‑sets: OOP bundles data and behavior; FP builds reusable, stateless functions.
| Aspect | OOP | Functional |
|---|---|---|
| Learning Curve | Familiar if you think in “things.” | Might feel abstract at first. |
| State Management | Built‑in via mutable objects. | Encourages immutability and safer concurrency. |
| Testing | Requires mocks for object state. | Simple: pure functions are easy to test. |
| Performance | May allocate more memory per object. | Often lighter but can incur overhead from frequent allocations. |
| Code Reuse | Inheritance and interfaces. | Small composable functions reused through composition. |
| Paradigm | Languages Well‑Suited |
|---|---|
| Object‑Oriented | Java, C++, C#, Python, Ruby, Swift, Kotlin |
| Functional | Haskell, Elixir, Clojure, F#, OCaml, Erlang |
| Multi‑Paradigm | JavaScript (with Ramda), Scala, Rust, Kotlin, Python, TypeScript |
Tip: Python supports both styles, making it a gentle playground to explore and mix paradigms.
Task class with properties like title, priority, and a TodoList class with methods add_task, remove_task, and mark_complete.add_task(list, task) and complete_task(list, task_id) that return new lists.Compare how each feels, especially when you add concurrency or unit tests.
Both Object‑Oriented and Functional Programming give you structured ways to break down complex problems. OOP mirrors real‑world objects, while FP treats computation like a pipeline of data transformations. Try re‑implementing a small project in both styles, notice the differences, and choose the paradigm—or blend—that helps you write clearer, safer, and more maintainable code.