Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def email_clients(clients: List[Client]) -> None:
**Good**:

```python
from typing import List
from typing import Iterator


class Client:
Expand All @@ -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**
Expand Down