Great.
DAY 16 sharpens your thinking about functional programming in Python — not as a style fad, but as a runtime + readability tradeoff.

After today:

  • You’ll know exactly when lambdas help and when they hurt
  • You’ll stop abusing map/filter
  • You’ll answer FP interview questions like a Pythonista, not like a Haskell clone

🧠 DAY 16 — Lambda & Functional Programming in Python

(map, filter, reduce, purity, tradeoffs)


🔑 CORE IDEA OF DAY 16

Python supports functional programming, but it is NOT a functional language.
Python favors clarity over functional purity.

Everything today is about intentional use, not ideology.


1️⃣ What Is a Lambda REALLY?

A lambda is:

  • An anonymous function
  • A function expression, not a statement
  • Limited to one expression
  • No statements, assignments, or annotations
lambda x: x + 1

Equivalent to:

def f(x):
    return x + 1

But with severe constraints.


2️⃣ Lambda Is a Function Object

f = lambda x: x * 2
  • f is a function object
  • Has __code__, __globals__
  • No name (__name__ == "<lambda>")

This matters for:

  • Debugging
  • Stack traces
  • Readability

3️⃣ Why Lambdas Are Restricted (Design Rationale)

Python intentionally restricts lambdas to:

  • Prevent unreadable code
  • Encourage named functions
  • Avoid complex one-liners

Guido’s rule:

“If it doesn’t fit on one line cleanly, don’t lambda it.”


4️⃣ Common (Bad) Lambda Abuse ❌

lambda x: (x := x + 1) * (x := x + 2)

Yes, it works.
No, you should never do this.

Interviewers penalize this style.


5️⃣ When Lambdas Are GOOD ✅

✅ Short, obvious transformations

sorted(data, key=lambda x: x.price)

✅ Simple callbacks

btn.on_click(lambda e: print("clicked"))

✅ Key functions

min(items, key=lambda x: x.age)

Rule of thumb:

If you can name it, name it.


6️⃣ map() vs List Comprehension (VERY IMPORTANT)

map(lambda x: x*x, data)

vs

[x*x for x in data]

Why list comprehensions are preferred:

  • More readable
  • Pythonic
  • Same or better performance
  • Easier debugging

Interviewers expect:

“Prefer comprehensions over map/filter in Python.”


7️⃣ filter() vs Comprehension

filter(lambda x: x > 0, data)

Better:

[x for x in data if x > 0]

Reason:

  • Condition is visible
  • Less cognitive load

8️⃣ reduce() — Powerful but Dangerous

from functools import reduce
reduce(lambda a, b: a + b, data)

Equivalent (better):

sum(data)

When reduce is justified:

  • Non-obvious accumulation
  • Custom fold logic
  • Domain-specific reduction

Otherwise:
❌ Avoid


9️⃣ Functional Programming Pillars (Python Perspective)

ConceptPython Reality
First-class functions
Higher-order functions
Pure functions⚠️ (discipline needed)
Immutability❌ (opt-in only)
Lazy evaluation⚠️ (generators)

Python supports FP — but doesn’t enforce it.


🔟 Purity, Side Effects & Reality

Pure function:

def f(x):
    return x * 2

Impure function:

def g(x):
    print(x)
    return x * 2

Python allows both — responsibility is on you.


11️⃣ Lambdas + Closures = Traps (REVISIT DAY 15)

funcs = [lambda: i for i in range(3)]

❌ Late binding again.

Fix:

funcs = [lambda i=i: i for i in range(3)]

Same closure rule applies.


12️⃣ Performance Reality (IMPORTANT)

  • Lambdas are not faster than functions
  • Overhead is the same
  • Readability is the deciding factor

Don’t micro-optimize lambdas.


🔥 INTERVIEW TRAPS (DAY 16)

Q1

f = lambda x: x + 1
print(type(f))

<class 'function'>


Q2

Why can’t lambda contain statements?

✔ Language design choice
✔ Readability
✔ Avoid complexity


Q3

Which is more Pythonic?

map(lambda x: x*x, lst)

or

[x*x for x in lst]

✔ Second (list comprehension)


Q4

Why does Python still keep map/filter/reduce?

✔ Functional completeness
✔ Interoperability
✔ Legacy + niche use cases


🧠 DAY 16 MENTAL CHECKLIST

Before using functional constructs:

  1. Is readability improved?
  2. Is intent obvious?
  3. Would a comprehension be clearer?
  4. Am I hiding logic in lambdas?
  5. Is this Pythonic or just clever?

📝 DAY 16 ASSIGNMENT (MANDATORY)

1️⃣ Predict output (NO RUNNING):

funcs = []
for i in range(3):
    funcs.append(lambda x=i: x)

print([f() for f in funcs])

2️⃣ Rewrite this more Pythonically:

from functools import reduce
reduce(lambda a, b: a + b, data)

3️⃣ Explain clearly:

  • Why Python discourages heavy functional style
  • When reduce is still appropriate

🔜 DAY 17 PREVIEW

DAY 17 — Modules, Imports & sys.path (How Python Finds Code)

You’ll learn:

  • How imports really work
  • Import caching
  • Circular imports (and how to break them)
  • __name__ == "__main__" demystified

When ready, say 👉 “START DAY 17”