পাইথনে কার্যকরী প্রোগ্রামিং বিশুদ্ধ ফাংশন, অপরিবর্তনীয়তা, ফাংশন রচনা এবং পার্শ্ব প্রতিক্রিয়া এড়ানোর উপর ফোকাস করে। 2026 সালে, পাইথনের কার্যকরী সরঞ্জামগুলি — মানচিত্র, ফিল্টার, হ্রাস, ফাংক্টুল এবং আইটারটুলগুলি — টাইপ ইঙ্গিতগুলির সাথে মিলিত হয়ে কার্যকরী পাইথন কোডকে পরিষ্কার এবং অভিব্যক্তিপূর্ণ করে তোলে। এই গাইড মূল নিদর্শন কভার.
মূল কার্যকরী ধারণা
বিশুদ্ধ ফাংশন
# Pure function: same inputs → same outputs, no side effects
def add(a: int, b: int) -> int:
return a + b # pure: no external state, no I/O
def get_tax(price: float, rate: float = 0.1) -> float:
return price * rate # pure
# Impure function (has side effects)
total = 0
def add_to_total(n: int) -> None: # modifies external state
global total
total += n # side effect!
# Impure: same call, different result
def get_current_time():
import time
return time.time() # side effect: depends on external state
উচ্চ ক্রম ফাংশন
from typing import Callable, TypeVar
T = TypeVar("T")
U = TypeVar("U")
# Functions that take or return functions
def compose(*fns: Callable) -> Callable:
from functools import reduce
return reduce(lambda f, g: lambda x: f(g(x)), fns)
def pipe(*fns: Callable) -> Callable:
return reduce(lambda f, g: lambda x: g(f(x)), fns)
# Usage
clean_name = compose(str.title, str.strip, str.lower)
print(clean_name(" ALICE SMITH ")) # "Alice Smith"
# Partial application
from functools import partial
def multiply(a: int, b: int) -> int:
return a * b
double = partial(multiply, 2)
triple = partial(multiply, 3)
numbers = [1, 2, 3, 4, 5]
print(list(map(double, numbers))) # [2, 4, 6, 8, 10]
# Memoize expensive functions
from functools import cache
@cache
def fib(n: int) -> int:
if n < 2: return n
return fib(n-1) + fib(n-2)
মানচিত্র, ফিল্টার, হ্রাস
from functools import reduce
users = [
{"name": "Alice", "age": 30, "active": True, "score": 8.5},
{"name": "Bob", "age": 25, "active": False, "score": 7.2},
{"name": "Carol", "age": 35, "active": True, "score": 9.1},
{"name": "Dave", "age": 28, "active": True, "score": 6.8},
]
# map: transform each element
names = list(map(lambda u: u["name"], users))
# filter: keep matching elements
active_users = list(filter(lambda u: u["active"], users))
# reduce: fold into single value
total_score = reduce(lambda acc, u: acc + u["score"], users, 0.0)
avg_score = total_score / len(users)
# Comprehensions (preferred over map/filter in Python)
names = [u["name"] for u in users]
active_users = [u for u in users if u["active"]]
total_score = sum(u["score"] for u in users)
# When to use map/filter vs comprehension:
# map/filter: when working with existing function references
# Comprehension: for new logic (more Pythonic, more readable)
squares = list(map(lambda x: x**2, range(10))) # map OK
squares = [x**2 for x in range(10)] # better
অপরিবর্তনীয়তা নিদর্শন
from dataclasses import dataclass, replace
from typing import NamedTuple
# Frozen dataclass — immutable
@dataclass(frozen=True)
class Point:
x: float
y: float
def translate(self, dx: float, dy: float) -> "Point":
return replace(self, x=self.x + dx, y=self.y + dy) # new object!
p1 = Point(1.0, 2.0)
p2 = p1.translate(3.0, 4.0) # p1 unchanged
# p1.x = 10 # raises FrozenInstanceError
# NamedTuple — immutable, fast
class User(NamedTuple):
id: int
name: str
email: str
user = User(1, "Alice", "alice@example.com")
updated = user._replace(name="Alice Smith") # new User
# Immutable dict update patterns
original = {"a": 1, "b": 2, "c": 3}
updated = {**original, "b": 99} # {a:1, b:99, c:3} — original unchanged
# list transformations that return new lists (don't mutate)
original = [1, 2, 3, 4, 5]
doubled = [x * 2 for x in original] # new list
filtered = [x for x in original if x > 2] # new list
sorted_list = sorted(original) # new list (sorted() vs .sort())
ফাংক্টুল এবং আইটারটুল সহ পাইপলাইন
from itertools import islice, chain, filterfalse
from functools import reduce
# Data pipeline as function composition
def process_orders(orders):
pipeline = [
# Filter: only completed orders
lambda orders: filter(lambda o: o["status"] == "completed", orders),
# Transform: calculate revenue per order
lambda orders: map(lambda o: {**o, "revenue": o["price"] * o["qty"]}, orders),
# Sort: by revenue descending
lambda orders: sorted(orders, key=lambda o: o["revenue"], reverse=True),
# Limit: top 10
lambda orders: islice(orders, 10),
]
return reduce(lambda data, fn: fn(data), pipeline, orders)
top_orders = list(process_orders(all_orders))
# Using toolz library for cleaner pipelines
from toolz import pipe, curry, compose
@curry
def filter_status(status, orders):
return filter(lambda o: o["status"] == status, orders)
@curry
def add_revenue(orders):
return map(lambda o: {**o, "revenue": o["price"] * o["qty"]}, orders)
result = pipe(
all_orders,
filter_status("completed"),
add_revenue,
list
)
কার্যকরী বনাম OOP — কখন প্রতিটি ব্যবহার করবেন
| যখন কার্যকরী ব্যবহার করুন | OOP কখন ব্যবহার করুন |
|---|---|
| ডেটা ট্রান্সফরমেশন পাইপলাইন | বাস্তব বিশ্বের সত্তা মডেলিং |
| রাষ্ট্রহীন অপারেশন | রাষ্ট্র এবং আচরণ একসাথে পরিচালনা করা |
| সমবর্তী/সমান্তরাল কোড | অনুক্রমিক সম্পর্ক (উত্তরাধিকার) |
| গাণিতিক গণনা | প্লাগইন/এক্সটেনশন আর্কিটেকচার |
পাইথন উভয় দৃষ্টান্তকে সমর্থন করে — যেটি সমস্যার সাথে মানানসই তা ব্যবহার করুন। বেশিরভাগ উত্পাদন পাইথন কোড উভয়ই মিশ্রিত করে: কাঠামোর জন্য ক্লাস, ডেটা প্রক্রিয়াকরণের জন্য কার্যকরী নিদর্শন।
2026 সালে পাইথন ফাংশনাল প্রোগ্রামিং: পরীক্ষাযোগ্যতার জন্য বিশুদ্ধ ফাংশন ব্যবহার করুন, পঠনযোগ্যতার জন্য মানচিত্র/ফিল্টারের উপর বোধগম্যতা, অপরিবর্তনীয় ডেটার জন্য হিমায়িত ডেটাক্লাস এবং পরিষ্কার রূপান্তর পাইপলাইনের জন্য কম্পোজ/পাইপ ব্যবহার করুন। কার্যকরী নিদর্শনগুলি ডেটা প্রক্রিয়াকরণে বিশেষভাবে মূল্যবান, যেখানে বিশুদ্ধ ফাংশন এবং সংমিশ্রণযোগ্য পাইপলাইনগুলি কোড পরীক্ষা করা এবং যুক্তি করা সহজ করে তোলে।
🔗 Share this article
✍️ Leave a Comment