Skip to content

gh-141984: Reword the "yield expressions"` section - #159421

Open
encukou wants to merge 17 commits into
python:mainfrom
encukou:expressions-yield-2
Open

encukou wants to merge 17 commits into
python:mainfrom
encukou:expressions-yield-2

Conversation

@encukou

@encukou encukou commented Oct 14, 2026 •

Copy link
Copy Markdown
Member
  • Move discussion on the runtime behaviour of generators to builtins/stdtypes.rst.
  • Reword and expand the section on yield expressions: start with an overview and common usage, then add details, end with formal grammar and examples.
  • De-emphasize using yield to implement coroutines (the use case is now served by async/await.
  • Copy an example from PEP-380 to keep the docs more self-contained
  • In Simple Statements, merge yield_stmt into expression_stmt

This PR stops before the section on async generators (yield in async functions); that's left for the next PR.

@read-the-docs-community

read-the-docs-community Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
@encukou

encukou commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

(I'll fix the lint issues later; hopefully they won't block review.)

@Robertboy18 Robertboy18 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this reads much better! A few small things I noticed while running the examples on 3.15 and main.


>>> def generator_function():
... value = yield 123
... print("value of yield is", value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The output below has a colon (value of yield is: None), but this prints value of yield is None:

Suggested change
... print("value of yield is", value)
... print("value of yield is:", value)

>>> iterator = adding_generator()
>>> next(iterator)
"first number"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The REPL shows the repr here:

Suggested change
"first number"
'first number'
>>> next(iterator)
"first number"
>>> iterator.send(1)
"second number"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
"second number"
'second number'

A :keyword:`yield` expression can only be used in the body of a
function definition.
More precisely, it can only be used when the closest enclosing *block*

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this also link to the annotation-scopes restrictions? For example, x: (yield) = 1 inside a function is rejected with SyntaxError: yield expression cannot be used within an annotation.


Note that ``yield_expression``, the unparenthesized rule, also appears in
:token:`~python-grammar:expression_stmt` and
:token:`~python-grammar:assignment_stmt`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

yield_expression also appears in augmented_assignment_stmt and annotated_assignment_stmt (x += yield 1 and x: int = yield 1 work without parentheses) and in f_expression in lexical_analysis.rst (f"{yield 1}"). Maybe list those here too? The f-string one would also need a bullet in the "They can be only omitted when" list above.

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

2 participants