Skip to content

Commit 72dc7ba

Browse files
committed
Fix return type annotation and explain generator usage
1 parent dfa0f7c commit 72dc7ba

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def email_clients(clients: List[Client]) -> None:
315315
**Good**:
316316

317317
```python
318-
from typing import List
318+
from typing import Iterator
319319

320320

321321
class Client:
@@ -326,19 +326,25 @@ def email(client: Client) -> None:
326326
pass
327327

328328

329-
def get_active_clients(clients: List[Client]) -> List[Client]:
330-
"""Filter active clients.
331-
"""
332-
return [client for client in clients if client.active]
329+
def active_clients(clients: Iterator[Client]) -> Iterator[Client]:
330+
"""Yield only active clients."""
331+
return (client for client in clients if client.active)
333332

334333

335-
def email_clients(clients: List[Client]) -> None:
336-
"""Send an email to a given list of clients.
337-
"""
338-
for client in get_active_clients(clients):
334+
def email_client(clients: Iterator[Client]) -> None:
335+
"""Send an email to a given list of clients."""
336+
for client in active_clients(clients):
339337
email(client)
338+
339+
**Why this is better:**
340+
341+
Using a generator avoids creating an intermediate list in memory.
342+
Clients are filtered lazily, which improves performance and scalability
343+
when working with large collections.
344+
340345
```
341346

347+
342348
Do you see an opportunity for using generators now?
343349

344350
**Even better**

0 commit comments

Comments
 (0)