File tree Expand file tree Collapse file tree 1 file changed +15
-9
lines changed
Expand file tree Collapse file tree 1 file changed +15
-9
lines changed Original file line number Diff line number Diff 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
321321class 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+
342348Do you see an opportunity for using generators now?
343349
344350** Even better**
You can’t perform that action at this time.
0 commit comments