return
return ends the current call immediately. No return means None; "return a, b" returns one tuple; a return inside finally overrides everything else.
return a value
return expression
bare return
return # same as return None
several values
return a, b # one tuple
Use for
Handing a result back; leaving a function early
Result
the call expression evaluates to the returned object (None by default)
Pairs with
def, if (early exit), tuple unpacking, finally
Watch out
a return in finally discards the try block’s return value and any exception
Demo
Live evaluation
return inside the loop exits at the first match. If nothing matches, the function falls off the end and returns None.
Try:
Inputs
itemslistcomma-separated
targetintvalue to find
Code
def find(items, target): for i, x in enumerate(items): if x == target: return i find([4, 8, 15, 8], 8)
Result
1
In the implicit None tab the result box shows None when nothing matched — the function never reached a return. In the finally tab, steps shows the finally block ran even though return came first; with 0 the ZeroDivisionError still escapes (finally runs, then the exception continues).
Syntax slots
| Name | Type | Required | Description |
|---|---|---|---|
| expression | expression | no (None) | Evaluated, then the function exits. A comma-separated list builds a tuple. Omitted → None. |
Common patterns
Guard clauses
Return early for the special cases, keep the main path unindented.
def price(order): if order is None: return 0 if order.free: return 0 return order.qty * order.unit
Return several values
Return a tuple, unpack at the call site.
def split_name(full): first, _, last = full.partition(" ") return first, last first, last = split_name("Ada Lovelace")
Named results
Past two or three values, a NamedTuple or dataclass is clearer than a bare tuple.
from typing import NamedTuple class Stats(NamedTuple): low: float high: float def stats(xs): return Stats(min(xs), max(xs))
Examples
1. Return a value
def square(x):
return x * x
square(7)
Returns
492. No return gives None
def hello():
x = 1
print(hello())
Returns
None3. Several values are a tuple
def pair():
return 1, 2
pair()
Returns
(1, 2)4. Code after return never runs
def f():
return 'done'
print('never')
f()
Returns
'done'5. return in a generator sets StopIteration.value
def gen():
yield 1
return 'finished'
g = gen()
next(g)
try:
next(g)
except StopIteration as e:
print(e.value)
Returns
finished6. A return in finally wins
def f():
try:
return 'from try'
finally:
return 'from finally'
f()
Returns
'from finally'7. return outside a function
compile('return 1', '<demo>', 'exec')
Returns
SyntaxError: 'return' outside functionPitfalls
1. return inside finally swallows the exception
A return in finally replaces whatever was in flight — including an exception. The error silently disappears. (Python 3.14 emits a SyntaxWarning for this.)
return in finally
def load(): try: return 1 / 0 finally: return 'ok' load()
'ok'
cleanup only
def load(): try: return 1 / 0 finally: print('cleanup') load()
cleanup
ZeroDivisionError: division by zero
2. return inside the loop too early
A return inside the loop body ends the whole function on the first pass.
returns on item 1
def total(xs): s = 0 for x in xs: s += x return s total([1, 2, 3])
1
after the loop
def total(xs): s = 0 for x in xs: s += x return s total([1, 2, 3])
6
3. Printing instead of returning
print shows a value but the caller still receives None.
print
def double(x): print(x * 2) r = double(5) r is None
10
True
return
def double(x): return x * 2 r = double(5) r
10
When to use
Use it
- Every function whose caller needs a result
- Early exit once the answer is known (guard clauses, search loops)
- Returning several related values as a tuple
Reach for something else
- Producing a sequence of values one at a time → yield
- Signalling failure with a special value like -1 → raise an exception
- return inside finally — it hides errors
Notes
CPython impl
Compiles to RETURN_VALUE (RETURN_CONST for a constant); with a pending finally, the value is kept while the finally block runs
Generators
In a generator, return value ends iteration and becomes StopIteration.value — this is what yield from evaluates to
Async generators
A non-empty return in an async generator is a SyntaxError: 'return' with value in async generator
FAQ
None. A function that ends without executing a return statement, or executes a bare return, returns None.
History
3.3
Generator functions can return a value; it is carried by StopIteration.value (PEP 380).
3.14
The compiler emits a SyntaxWarning when a return, break or continue appears in a finally block.