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**