Lab 7: Function Frames with Memory Diagrams

The parameters and local variables within a function call frame, as well as any new lists created while executing the function body in a function call frame, provide new ways in which lists can be aliased/shared.

We explore this in the context of a Midterm 2 exam problem from a few semesters ago. The problem involves the following modifyLists function:

def modifyLists(L1, L2):
    L3 = L1[0:2] # one new list!
    L3.append(L2)
    print('L3a', L3) # 1st print
    L1[0] = L2[1]
    L2.insert(1, L1.pop(2))
    L1.append(8)
    print('L1', L1) # 2nd print 
    print('L2', L2) # 3rd print
    print('L3b', L3) # 4th print

Scenario 1

Draw a memory diagram to predict what is printed by the call to modifyLists in the following code:

X = [3, 4, 5]
Y = [6, 7]
modifyLists(X, Y)

Then test your prediction by copying the function definition and code into Thonny and running it.

Scenario 2

Draw a memory diagram to predict what is printed by the call to modifyLists in the following code:

Z = [7, 5, 3]
modifyLists(Z, Z)

Then test your prediction by copying the function definition and code into Thonny and running it.

Table of Contents