Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword return

Leave the current function with the evaluated expression, or None when no expression is given.

Version: Python 3.14; repository image, production runtime not attested

Syntax

return expression

Leave the current function with the evaluated expression, or None when no expression is given.

A finally suite on the exit path still runs. In a generator, return ends iteration and carries its value in StopIteration.value instead of yielding another item. An async generator permits only bare return.

Even return None is a SyntaxError because it includes an expression.

Examples

def empty():
    return
def stream():
    yield "row"
    return 4
g = stream()
print(empty(), next(g))
try:
    next(g)
except StopIteration as done:
    print(done.value)
try:
    compile("async def bad():\n    yield 1\n    return None", "<example>", "exec")
except SyntaxError:
    print("async return rejected")

Result

None row
4
async return rejected

Even `return None` is a SyntaxError because it includes an expression.

Sources