🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

पायथन फंक्शनल प्रोग्रामिंग गाइड 2026: प्योर फ़ंक्शंस, मैप और पाइपलाइन

⏱️3 min read  ·  641 words

पायथन में कार्यात्मक प्रोग्रामिंग शुद्ध कार्यों, अपरिवर्तनीयता, कार्य संरचना और दुष्प्रभावों से बचने पर केंद्रित है। 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 का उपयोग कब करें
डेटा परिवर्तन पाइपलाइन वास्तविक दुनिया की संस्थाओं की मॉडलिंग करना
राज्यविहीन संचालन राज्य और व्यवहार को एक साथ प्रबंधित करना
समवर्ती/समानांतर कोड पदानुक्रमित रिश्ते (विरासत)
गणितीय गणनाएँ प्लगइन/एक्सटेंशन आर्किटेक्चर

पायथन दोनों प्रतिमानों का समर्थन करता है – जो भी समस्या के अनुकूल हो उसका उपयोग करें। अधिकांश उत्पादन पायथन कोड दोनों को मिलाते हैं: संरचना के लिए कक्षाएं, डेटा प्रोसेसिंग के लिए कार्यात्मक पैटर्न।

2026 में पायथन कार्यात्मक प्रोग्रामिंग: परीक्षण योग्यता के लिए शुद्ध कार्यों का उपयोग करें, पठनीयता के लिए मानचित्र/फ़िल्टर पर समझ, अपरिवर्तनीय डेटा के लिए जमे हुए डेटाक्लास, और स्वच्छ परिवर्तन पाइपलाइनों के लिए कंपोज़/पाइप का उपयोग करें। डेटा प्रोसेसिंग में कार्यात्मक पैटर्न विशेष रूप से मूल्यवान होते हैं, जहां शुद्ध फ़ंक्शन और कंपोज़ेबल पाइपलाइन कोड को परीक्षण करना और तर्क करना आसान बनाते हैं।

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা