How Python Executes Code
However if you place this code in a Python script and this error occurs, your script immediately stops in its tracks with a traceback. It does not execute the following statement.
Execution Can Stop Suddenly
When Python executes a script, an error can interrupt the script before the next statement runs. The script stops and displays a traceback instead of continuing to the following statement. This makes execution errors important control-flow events: they do not merely report a problem; they change what the script does next.
The most important execution rule in this topic is simple: when an error occurs in a script and execution is not successfully recovered, Python stops before executing the following statement.
A Function Call in Motion
A function call temporarily moves execution into the function so the function can work with the supplied arguments. In the source example, the addtwo function is called with 3 and 5. Inside the function, the parameters a and b receive 3 and 5 respectively. The function adds those values, stores the sum in its local variable named added, and uses return to send the computed result back to the calling code. The calling code assigns that result to x and then prints x.
Following the addtwo result
Track the value as the addtwo function is called with 3 and 5.
Arguments supplied: The calling code supplies 3 and 5 to addtwo.
Parameters receive values: Inside the function, a receives 3 and b receives 5.
Sum computed: The function computes the sum and places it in the local variable added.
Result returned: The return statement sends the computed result back to the calling code.
Result used: The calling code assigns the returned result to x and prints x.
The printed result is 8 because the function was called with 3 and 5.
This example shows several stages of execution: values enter a function as arguments, the function works with its parameters and local variable, return sends a value outward, and the calling code uses the returned value. Following these stages is a useful way to understand what Python is doing rather than viewing a function call as a single unexplained action.
The Error Path
The error path has two visible results. First, Python produces a traceback. Second, execution stops, so the statement that follows the error is not executed. The traceback is therefore associated with the point where the script failed, while the missing output from the following statement is evidence that control never reached it.
Mistakes About Control Flow
Assuming the statement after an error still runs.
The script stops when the error occurs, so Python does not execute the following statement.
Fix:
Use the traceback to identify the failure point, then reason from that point onward: statements after the failure have not been reached.Treating a function call as if it produces its result without intermediate steps.
The source example obtains 8 through a sequence: addtwo receives 3 and 5, computes the sum in added, returns it, and the calling code assigns it to x.
Fix:
Trace the values through the function and then through the calling code.Ignoring the traceback after invalid input causes failure.
The traceback accompanies the script stopping at the error.
Fix:
Read the traceback as evidence that execution failed before the following statement.
Practice the Trace
Explain the execution of the addtwo example in your own words. Identify the values supplied as arguments, the values received by a and b, the local variable that stores the sum, the action that sends the result back, and the variable that receives the result in the calling code.
Hints
- Start with the values 3 and 5.
- Separate what happens inside the function from what happens in the calling code.
- End by explaining why the printed result is 8.
A script receives invalid input and fails with an error message. Predict what happens to the statement immediately after the failure, then explain how the traceback helps you reason about the point where execution stopped.
Hints
- The script does not continue past the error.
- The following statement is not executed.
- Connect the traceback with the failure point rather than treating it as ordinary program output.
Execution Takeaways
- A function call passes argument values to parameters, performs its work, and can return a result to the calling code.
- In the addtwo example, 3 and 5 produce 8; the result is returned, assigned to x, and printed.
- An error in a Python script produces a traceback and stops execution at the failure.
- The statement following an uncaught error is not executed.
- A traceback helps connect the failure with the point where script execution stopped.
Key Takeaways
- Python execution can be understood by tracing values and control flow from one step to the next.
- A function receives arguments through its parameters, computes a result, and can return that result to the calling code.
- When an error occurs in a script, Python displays a traceback and stops before the following statement.
- The absence of the following statement's execution is a direct consequence of the script stopping at the error.