gh-141984: Reword the Generator expressions section#150518
Conversation
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Documentation build overview
88 files changed ·
|
| 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. |
| >>> 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:
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 |
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>
Continuing after #148622, this rewords the Generator Expressions with increasingly complex examples, ending with the formal grammar (which is trivial in this case).