Skip to content

gh-141984: Reword the Generator expressions section - #150518

Merged
encukou merged 5 commits into
python:mainfrom
encukou:expressions-genexp-sq
Jun 10, 2026
Merged

gh-141984: Reword the Generator expressions section#150518
encukou merged 5 commits into
python:mainfrom
encukou:expressions-genexp-sq

Conversation

@encukou

@encukouencukou commented May 27, 2026

Copy link
Copy Markdown
Member

Continuing after #148622, this rewords the Generator Expressions with increasingly complex examples, ending with the formal grammar (which is trivial in this case).

Co-authored-by: Blaise Pabon <blaise@gmail.com>
@read-the-docs-community

read-the-docs-communityBot commented May 27, 2026

Copy link
Copy Markdown

Comment threadDoc/reference/expressions.rst Outdated
Comment threadDoc/reference/expressions.rst Outdated
Comment threadDoc/reference/expressions.rst Outdated
Comment threadDoc/reference/expressions.rst Outdated
Comment threadDoc/reference/expressions.rst Outdated
Comment on lines +983 to +984
The enclosing parentheses can be omitted in calls with only one
positional argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The enclosing parentheses can be omitted in calls with only one
positional argument.
The enclosing parentheses can be omitted in calls when it is
the only argument.

Proof by counterexample: sum(x ** 2 for x in range(10), start=3) has only one pos-arg.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single argument must be positional. For example, sum(iterable=x ** 2 for x in range(10)) is invalid syntax.

Would this work?

Suggested change
The enclosing parentheses can be omitted in calls with only one
positional argument.
The enclosing parentheses can be omitted in calls when the generator
expression is the only positional argument and there are no keyword
arguments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine.

Comment threadDoc/reference/expressions.rst Outdated
>>> list(iterator)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Thus, the example above is roughly equivalent to defining and calling

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it less rough, we could pass an iterator?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean; where should we pass it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like:

defmake_generator_of_squares(iterator):
forxiniterator:
yieldx**2make_generator_of_squares(iter(range(10)))

That way, it's also evaluated immediately like in generator expressions.

: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, and the
:term:`iterator` is immediately created for that iterable, so that an error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's moved around and reworded, see "The iterable expression in the leftmost :keyword:!for clause is evaluated immediately" in the new text.
For "Subsequent for clauses" the new wording is "All other expressions" -- it's for, if but also the "result" expression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my unclear comment, I was referring specifically to keeping the sentence on L972: "iterator is immediately created for that iterable"

(Which in turn made me think of the comment above :-)

@StanFromIrelandStanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"".join((c for c in ['L', 'G', 'T', 'M']))

@encukou
encukou merged commit 7bbb960 into python:mainJun 10, 2026
34 checks passed
@encukou
encukou deleted the expressions-genexp-sq branch June 10, 2026 14:45
@github-project-automationgithub-project-automationBot moved this from Todo to Done in Docs PRsJun 10, 2026
@encukouencukou added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 10, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @encukou, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7bbb9607a2e7d1f8a7de11ce02f0a2402d6e7262 3.14

@bedevere-app

Copy link
Copy Markdown

GH-151261 is a backport of this pull request to the 3.15 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 10, 2026
encukou added a commit that referenced this pull request Jun 10, 2026
GH-151261)
(cherry picked from commit 7bbb960)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
@encukou

Copy link
Copy Markdown
MemberAuthor

Like with the earlier PR, I'll concentrate on the next section rather than backport to 3.14.

@encukouencukou removed the needs backport to 3.14 bugs and security fixes label Jun 10, 2026
@bedevere-app

Copy link
Copy Markdown

GH-151267 is a backport of this pull request to the 3.14 branch.

@StanFromIreland

Copy link
Copy Markdown
Member

Like with the earlier PR, I'll concentrate on the next section rather than backport to 3.14.

Took less than a minute so I took care of it for 'ya.

encukou added a commit that referenced this pull request Jun 17, 2026
GH-151267)
(cherry picked from commit 7bbb960)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
…50518)
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docsDocumentation in the Doc dirskip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants

@encukou@StanFromIreland