discussion 1 quiz
control and environments

Question 1

Are these primitive expressions or call expressions?

Toggle Solution

The last one’s tricky! Try typing these two into a Python interpreter to convince yourself:

>>> 3 + 4
>>> (3).__add__(4)

Question 2

When do we make a new frame in an environment diagram?

Toggle Solution

We make a new frame whenever we call a user-defined function. This means we don’t create frames for builtin function calls like abs(x) and 3 + 4. We also don’t create frames for imported functions!

Question 3

Imagine we run the following lines through a Python interpreter. Draw the environment diagram after executing the program.

def swap(x, y):
    x, y = y, x
    return print('Swapped!', x, y)

x, y = 60, 1
a = swap(x, y)
swap(a, y)

Toggle Solution