Uh oh!
There was an error while loading. Please reload this page.
gh-141984: Reword the Generator expressions section - #150518
Conversation
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Documentation build overview
88 files changed · |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| The enclosing parentheses can be omitted in calls with only one | ||
| positional argument. |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
The single argument must be positional. For example, sum(iterable=x ** 2 for x in range(10)) is invalid syntax.
Would this work?
| 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. |
Uh oh!
There was an error while loading. Please reload this page.
| >>> list(iterator) | ||
| [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | ||
| Thus, the example above is roughly equivalent to defining and calling |
There was a problem hiding this comment.
To make it less rough, we could pass an iterator?
There was a problem hiding this comment.
I'm not sure what you mean; where should we pass it?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
I think we should keep this.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
StanFromIreland
left a comment
There was a problem hiding this comment.
"".join((c for c in ['L', 'G', 'T', 'M']))
Uh oh!
There was an error while loading. Please reload this page.
Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14. |
Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15. |
Sorry, @encukou, I could not cleanly backport this to |
GH-151261 is a backport of this pull request to the 3.15 branch. |
encukou
commented
Jun 10, 2026
Like with the earlier PR, I'll concentrate on the next section rather than backport to 3.14. |
GH-151267 is a backport of this pull request to the 3.14 branch. |
StanFromIreland
commented
Jun 10, 2026
Took less than a minute so I took care of it for 'ya. |
…50518) Co-authored-by: Blaise Pabon <blaise@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
Continuing after #148622, this rewords the Generator Expressions with increasingly complex examples, ending with the formal grammar (which is trivial in this case).