Lab 4
1 Stack frame tracing
1.1 hours_to_seconds(20)
3 stack frames are created when executing hours_to_seconds(20)
:
hours_to_seconds
hours_to_minutes
minutes_to_seconds
1.2 days_to_seconds(6)
5 stack frames are created when executing days_to_seconds(6)
:
days_to_seconds
days_to_hours
hours_to_seconds
hours_to_minutes
minutes_to_seconds
1.3 total
6 stack frames (including global frame) are created in total:
days_to_seconds
days_to_hours
hours_to_seconds
hours_to_minutes
minutes_to_seconds
2 Stack Frame Visualization
(every result matches the expectation)
2.1 hours_to_seconds(20)
running result:
2.2 days_to_seconds(6)
running result:
3 Updating list in place
3.1 trace the code manually
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_lis
t function on list1
The reason is that in Python, lists are mutable objects, and variables store references to objects rather than the objects themselves. When we assign list1
to list3
, we are actually making list3
reference the same list object as list1
. So when we modify list1
, list3
also reflects those changes because they both refer to the same list object.
3.3
This example demonstrates the concept of references. When we assign a mutable object to multiple variables, those variables actually reference the same object. Therefore, modifications made to one variable will affect other variables that reference the same object.
3.4
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 object reference. This is because the function parameter passes 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
There is 1 global variable in the program named num
with a value of 10.
4.2
There are 5 local variables in the program.
Inside the function multiply_local
, there are three local variables named:
num
with a value of 40
number
with a value of 10
multiple
with a value of 4
Inside the function multiply_global
:
number
with a value of 10
multiple
with a value of 4
4.3
The names used as parameter names in the program are number
and multiple
.
4.4
Inside either of the 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: (every result matches the expectation)