1.2.36 · D3 · HinglishIntroduction to Programming (Python)

Worked examplesGenerator expressions — memory efficiency

2,880 words13 min read↑ Read in English

1.2.36 · D3 · Coding › Introduction to Programming (Python) › Generator expressions — memory efficiency

Tum parent note se ek-line idea already jaante ho: ek generator expression items ko ek ek karke, demand par produce karta hai. Yeh page ek drill hai. Hum us idea ko lete hain aur use har tarah ki situation se takraate hain — normal cases, empty cases, infinite cases, "consumed twice" trap, ek real-world file problem, aur ek exam-style twist.

Shuru karne se pehle, ek promise: har symbol aur har function jo appear karta hai (sum, next, any, zip, itertools.islice, memory in bytes) — use pehli baar aane par explain kiya jayega. Agar tumne zindagi mein kabhi Python type nahi ki, line one se shuru karo aur phir bhi samajh mein aayega.


The scenario matrix

Generator expression ko ek machine ki tarah socho jisme ek input hota hai (koi iterable), ek rule hota hai (expr), ek optional filter hota hai (if cond), aur ek consumer hota hai (jo bhi values bahar kheenchta hai). In chaaron parts mein se har ek normal, empty, degenerate, ya limit tak pushed ho sakta hai. Neeche diya table har case-class list karta hai jo is topic mein aa sakti hai. Har cell ka ek worked example neeche diya gaya hai.

# Case class Isme kya khaas hai Example
A Normal, fully consumed ordinary rule, ordinary consumer Ex 1
B Filter removes some if cond elements drop karta hai Ex 2
C Filter removes ALL (degenerate) generator kuch yield nahi karta Ex 3
D Empty input (degenerate) iterable mein zero items hain Ex 3
E Partial consumption next se sirf kuch hi pull karo Ex 4
F Double consumption (the trap) ek pass ke baad exhausted ho jaata hai Ex 5
G Infinite / limiting input kabhi khatam nahi hoti; slice karna padta hai Ex 6
H Real-world word problem huge file, memory matters Ex 7
I Exam twist nested / lazy-capture gotcha Ex 8

Pehle do supporting pictures, phir examples.

Figure — Generator expressions — memory efficiency

Upar wala figure memory ke liye mental model hai (case classes A–H sab iske andar aate hain). List har box pehle se bana leti hai; generator exactly ek box hold karta hai aur recipe yaad rakhta hai. Flat green line dekho — woh hai, yaani "memory ke saath nahi badhti". ( aur ko Big-O space complexity mein explain kiya gaya hai; yahan inhe sirf "constant" vs " ke proportion mein badhne wala" padho.)

Figure — Generator expressions — memory efficiency

Doosra figure consumption ke liye mental model hai: ek generator ka ek pointer hota hai jo sirf aage badhta hai. Woh single arrow cases E, F aur G ko ek saath explain karta hai — tum jaldi rok sakte ho (E), tum khatam ho sakte ho (F), aur tum forever chal sakte ho (G) — lekin tum kabhi rewind nahi kar sakte.


The worked examples



Active Recall

Recall Answers cover karo aur self-test karo

sum(x*x for x in range(5)) se kya print hota hai? ::: 30 list(n**3 for n in range(6) if n % 2 == 0) kya deta hai? ::: [0, 8, 64] list(g) se g fully consume hone ke baad, sum(g) kya return karta hai? ::: 0 — g exhausted ho gaya (m*m for m in count(1)) par islice kyun use karna padta hai? ::: Source infinite hai; plain list kabhi finish nahi hoti aur memory khatam ho jaati funcs = [lambda: i for i in range(3)] mein, [f() for f in funcs] kya print karta hai? ::: [2, 2, 2]i call time par read hota hai, jab loop pehle hi khatam ho chuki hoti hai any("FATAL" in line for line in f) file padhna kab band karta hai? ::: Pehli matching line par — yeh short-circuit karta hai


Connections

  • Generator expressions — memory efficiency — parent idea jise yeh page drill karta hai
  • Lazy evaluation — "computed on demand" Examples 4, 6, 8 ko power karta hai
  • Iterators and the iterator protocolnext() aur forward-only pointer
  • Generator functions and yielddef/yield cousin, same laziness
  • List comprehensions — eager alternative jo Examples 2, 8 mein use hua
  • Big-O space complexity vs jo pehle figure mein dikhaya gaya
  • Memory management in Python — kyun huge-file case (Ex 7) matter karta hai

Generator expression

if cond pehle run hota hai

expr survivors ko transform karta hai

Fully consumed A B

Empty ya sab filtered C D

next se pull kiya E

Doosra pass empty F

Infinite source ko islice chahiye G

Huge file any short circuit H

Value call time par read hoti hai I