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

@encukou encukou 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-community Bot commented May 27, 2026

Copy link
Copy Markdown

Comment thread Doc/reference/expressions.rst Outdated
Comment thread Doc/reference/expressions.rst Outdated
Comment thread Doc/reference/expressions.rst Outdated
Comment thread Doc/reference/expressions.rst Outdated
Comment thread Doc/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
Member Author

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 thread Doc/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
Member Author

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:

def make_generator_of_squares(iterator):
   for x in iterator:
       yield x ** 2

make_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
Member Author

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 :-)

@StanFromIreland StanFromIreland 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:main Jun 10, 2026
34 checks passed
@encukou encukou deleted the expressions-genexp-sq branch June 10, 2026 14:45
@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Jun 10, 2026
@encukou encukou 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

bedevere-app Bot commented Jun 10, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot 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

encukou commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

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

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

bedevere-app Bot commented Jun 10, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants