return — Expert Examples
Exits a function and optionally sends a value back to the caller
Return bytecode and optimization
How return compiles and interacts with the frame.
python
import dis def simple(): return 42 def implicit(): x = 42 print("Explicit return:") dis.dis(simple) print("\nImplicit return (None):") dis.dis(implicit) # Return pushes value onto stack then RETURN_VALUE pops it # Implicit return adds LOAD_CONST None + RETURN_VALUE # Inspect return value behavior print(f"\nsimple() returns: {simple()}") print(f"implicit() returns: {implicit()}") print(f"implicit() is None: {implicit() is None}")
Every function ends with RETURN_VALUE bytecode. Functions without explicit return get an implicit 'return None' — the compiler adds LOAD_CONST None before RETURN_VALUE.
Want to try these examples interactively?
Open Expert Playground