From 72dc7ba915ffa37f4c88a55e0f91da8bfd3bbda2 Mon Sep 17 00:00:00 2001 From: Sangeeth PS Date: Sun, 28 Dec 2025 23:06:42 +0530 Subject: [PATCH] Fix return type annotation and explain generator usage --- README.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cae487b..51cd7ab 100644 --- a/README.md +++ b/README.md @@ -315,7 +315,7 @@ def email_clients(clients: List[Client]) -> None: **Good**: ```python -from typing import List +from typing import Iterator class Client: @@ -326,19 +326,25 @@ def email(client: Client) -> None: pass -def get_active_clients(clients: List[Client]) -> List[Client]: - """Filter active clients. - """ - return [client for client in clients if client.active] +def active_clients(clients: Iterator[Client]) -> Iterator[Client]: + """Yield only active clients.""" + return (client for client in clients if client.active) -def email_clients(clients: List[Client]) -> None: - """Send an email to a given list of clients. - """ - for client in get_active_clients(clients): +def email_client(clients: Iterator[Client]) -> None: + """Send an email to a given list of clients.""" + for client in active_clients(clients): email(client) + +**Why this is better:** + +Using a generator avoids creating an intermediate list in memory. +Clients are filtered lazily, which improves performance and scalability +when working with large collections. + ``` + Do you see an opportunity for using generators now? **Even better**