Skip to content

gh-141984: Reword the Generator expressions section#150518

Open
encukou wants to merge 2 commits into
python:mainfrom
encukou:expressions-genexp-sq
Open

gh-141984: Reword the Generator expressions section#150518
encukou wants to merge 2 commits into
python:mainfrom
encukou:expressions-genexp-sq

Conversation

@encukou
Copy link
Copy Markdown
Member

@encukou encukou commented May 27, 2026

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
Copy link
Copy Markdown

read-the-docs-community Bot commented May 27, 2026

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

Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants