Understanding Python Functions: Stack Frames, List Mutability, and Variable Scope
1 Stack Frame Tracing
1.1 hours_to_seconds(20)
When executing hours_to_seconds(20), 3 stack frames are created:
hours_to_secondshours_to_minutesminutes_to_seconds
1.2 days_to_seconds(6)
When executing days_to_seconds(6), 5 stack frame are created:
days_to_secondsdays_to_hourshours_to_secondshours_to_minutesminutes_to_seconds
1.3 Total Stack Frames
In total, 6 stack frames (including the global frame) are created:
days_to_secondsdays_to_hourshours_to_secondshours_to_minutesminutes_to_seconds
2 Stack Frame Visualization
(All results match expectations.)
2.1 hours_to_seconds(20)
Running result:


2.2 days_to_seconds(6)
Running result:



3 Updating a List In-Place
3.1 Manual Code Trace
before update
list1: [0, 1, 2, 3]
list2: [0, 1, 2, 3]
list3: [0, 1, 2, 3]
after update
list1: [0, 1, 4, 9]
list2: [0, 1, 2, 3]
list3: [0, 1, 4, 9]
3.2 Running Result

list2 did not change, while list3 did change after calling the update_list function on list1.
The reason is that in Python, lists are mutable objects, and variables store references to objectss rather than the objects themselves. When we assign list1 to list3, we are actually making list3 reference the same list object as list1. Therefore, when we modify list1, list3 also reflects those changes because they both refer to the same list object.
3.3 Explanation
This example demonstrates the concept of references. When we assign a mutable object to multiple variables, those variables actually reference the same object. Consequently, modifications made to one variable will affect other variables that reference the same object.
3.4 Implications for Function Calls
Regarding object references in function calls, when we pass a mutable object as a parameter to a function, the function can modify the object because it operates on the same reference. This is because the parameter receives the reference to the object, not a copy of the object itself. Hence, for mutable objects, modifications made within the function persist after the function call. However, for immutable objects, modifications made within the function do not affect the variables outside the function, because immutable objects create a new object when modified.
4 Global vs Local Variables
4.1 Global Variable
There is 1 global variable in the program named num with a value of 10.
4.2 Local Variables
There are 5 local variables in the program.
Inside the function multiply_local, there are three local variables:
numwith a value of 40numberwith a value of 10multiplewith a value of 4
Inside the function multiply_global:
numberwith a value of 10multiplewith a value of 4
4.3 Parameter Names
The names used as parameter names in the program are number and multiple.
4.4 Variable Behavior
Inside either function call, the value of the variable num will always be 40.
After the multiply_local function call, the value of the global variable num remains unchanged at 10.
After the multiply_global function call, the value of the global variable num will be modified to 40.
Therefore, the expected output is:
num inside function: 40
num after multiple_local: 10
num inside function: 40
num after multiple_glocal: 40
Running result: (Matches expectations.)

Note: This article is based on a Python introductory lab exercise focusing on stack frames, mutable objects, and variable scope.