From 274bfee738370de732f05df535b9afdbee5ea81f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 10 Jun 2025 17:02:16 +0300 Subject: [PATCH 1/2] gh-135171: Update documentation for the generator expression Document that the iterator for the leftmost "for" clause is created immediately. --- Doc/reference/expressions.rst | 3 ++- .../2025-06-10-17-02-06.gh-issue-135171.quHvts.rst | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Documentation/2025-06-10-17-02-06.gh-issue-135171.quHvts.rst diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 2a550b504ca765..8ae063328a90e2 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -406,7 +406,8 @@ brackets or curly braces. Variables used in the generator expression are evaluated lazily when the :meth:`~generator.__next__` method is called for the generator object (in the same fashion as normal generators). However, the iterable expression in the -leftmost :keyword:`!for` clause is immediately evaluated, so that an error +leftmost :keyword:`!for` clause is immediately evaluated, and the +:term:`iterator` is immediately created for that iterable, so that an error produced by it will be emitted at the point where the generator expression is defined, rather than at the point where the first value is retrieved. Subsequent :keyword:`!for` clauses and any filter condition in the leftmost diff --git a/Misc/NEWS.d/next/Documentation/2025-06-10-17-02-06.gh-issue-135171.quHvts.rst b/Misc/NEWS.d/next/Documentation/2025-06-10-17-02-06.gh-issue-135171.quHvts.rst new file mode 100644 index 00000000000000..129ff74189bc64 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2025-06-10-17-02-06.gh-issue-135171.quHvts.rst @@ -0,0 +1,2 @@ +Document that the :term:`iterator` for the leftmost :keyword:`!for` clause +in the generator expression is created immediately. From 34d791faf1ea9a3dde45196d1c381952f065d73d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 10 Jun 2025 19:31:48 +0300 Subject: [PATCH 2/2] Update Doc/reference/expressions.rst Co-authored-by: Brian Skinn --- Doc/reference/expressions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 8ae063328a90e2..3d3bf1d9840eef 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -408,7 +408,7 @@ Variables used in the generator expression are evaluated lazily when the fashion as normal generators). However, the iterable expression in the leftmost :keyword:`!for` clause is immediately evaluated, and the :term:`iterator` is immediately created for that iterable, so that an error -produced by it will be emitted at the point where the generator expression +produced while creating the iterator will be emitted at the point where the generator expression is defined, rather than at the point where the first value is retrieved. Subsequent :keyword:`!for` clauses and any filter condition in the leftmost :keyword:`!for` clause cannot be evaluated in the enclosing scope as they may