-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
51 lines (38 loc) · 1.13 KB
/
Copy pathexample.py
File metadata and controls
51 lines (38 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Generator examples — lazy iteration with yield."""
def countdown(n):
while n > 0:
yield n
n -= 1
def fibonacci(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
def read_large_file_lines(path):
"""Memory-efficient line-by-line reading."""
with open(path) as f:
for line in f:
yield line.rstrip("\n")
def send_example():
"""Demonstrate two-way communication via .send()."""
total = 0
while True:
value = yield total
if value is None:
break
total += value
if __name__ == "__main__":
print("=== countdown ===")
for i in countdown(5):
print(i, end=" ")
print()
print("\n=== fibonacci (< 100) ===")
print(list(fibonacci(100)))
print("\n=== generator expression vs list comprehension ===")
gen = (x * x for x in range(1_000_000)) # lazy — no list in memory
print(f"First 5 squares: {[next(gen) for _ in range(5)]}")
print("\n=== .send() ===")
gen = send_example()
next(gen) # prime the generator
print(gen.send(10)) # 10
print(gen.send(20)) # 30