Lab 6: Nonlocal & Mutability
Due at 11:59pm on Friday, 7/19/2019.
Starter Files
Download lab06.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.
Submission
By the end of this lab, you should have submitted the lab with
python3 ok --submit
. You may submit more than once before the
deadline; only the final submission will be graded.
Check that you have successfully submitted your code on
okpy.org.
- To receive credit for this lab, you must complete questions 1 and 2 and submit through Ok.
- The remaining questions are optional. It is recommended that you complete these problems in your own time.
Topics
Consult this section if you need a refresher on the material for this lab. It's okay to skip directly to the questions and refer back here should you get stuck.
Nonlocal
We say that a variable defined in a frame is local to that frame. A variable is nonlocal to a frame if it is defined in the environment that the frame belongs to but not the frame itself, i.e. in its parent or ancestor frame.
So far, we know that we can access variables in parent frames:
def make_adder(x):
""" Returns a one-argument function that returns the result of
adding x and its argument. """
def adder(y):
return x + y
return adder
Here, when we call make_adder
, we create a function adder
that is able to
look up the name x
in make_adder
's frame and use its value.
However, we haven't been able to modify variable in parent frames. Consider the following function:
def make_withdraw(balance):
"""Returns a function which can withdraw
some amount from balance
"""
>>> withdraw = make_withdraw(50)
>>> withdraw(25)
25
>>> withdraw(25)
0
"""
def withdraw():
if amount > balance:
return "Insufficient funds"
balance = balance - amount
return balance
return withdraw
The inner function withdraw
attempts to update the variable balance
in its
parent frame. Running this function's doctests, we find that it causes the
following error:
UnboundLocalError: local variable 'balance' referenced before assignment
Why does this happen? When we execute an assignment statement, remember that we
are either creating a new binding in our current frame or we are updating an
old one in the current frame. For example, the line balance = ...
in withdraw
,
is creating the local variable balance
inside withdraw
's frame. This
assignment statement tells Python to expect a variable called balance
inside
withdraw
's frame, so Python will not look in parent frames for this variable.
However, notice that we tried to compute balance - amount
before the local variable
was created! That's why we get the UnboundLocalError
.
To avoid this problem, we introduce the nonlocal
keyword. It allows us to
update a variable in a parent frame!
Some important things to keep in mind when using
nonlocal
nonlocal
cannot be used with global variables (names defined in the global frame).- If no nonlocal variable is found with the given name, a
SyntaxError
is raised.- A name that is already local to a frame cannot be declared as nonlocal.
Consider this improved example:
def make_withdraw(balance):
"""Returns a function which can withdraw
some amount from balance
"""
>>> withdraw = make_withdraw(50)
>>> withdraw(25)
25
>>> withdraw(25)
0
"""
def withdraw(amount):
nonlocal balance
if amount > balance:
return "Insufficient funds"
balance = balance - amount
return balance
return withdraw
The line nonlocal balance
tells Python that balance
will not be local to this frame, so it will look for it in parent frames. Now we can update balance
without running into problems.
Mutability
We say that an object is mutable if its state can change as code is executed. The process of changing an object's state is called mutation. Examples of mutable objects include lists and dictionaries. Examples of objects that are not mutable include tuples and functions.
We have seen how to use the ==
operator to check if two expressions evaluate
to equal values. We now introduce a new comparison operator, is
, that
checks whether two expressions evaluate to the same values.
Wait, what's the difference? For primitive values, there is none:
>>> 2 + 2 == 3 + 1
True
>>> 2 + 2 is 3 + 1
True
This is because all primitives have the same identity under the hood. However, with non-primitive values, such as lists, each object has its own identity. That means you can construct two objects that may look exactly the same but have different identities.
>>> lst1 = [1, 2, 3, 4]
>>> lst2 = [1, 2, 3, 4]
>>> lst1 == lst2
True
>>> lst1 is lst2
False
Here, although the lists referred to by lst1
and lst2
have equal
contents, they are not the same object. In other words, they are the same in
terms of equality, but not in terms of identity.
This is important in our discussion of mutability because when we mutate an object, we simply change its state, not its identity.
>>> lst1 = [1, 2, 3, 4]
>>> lst2 = lst1
>>> lst1.append(5)
>>> lst2
[1, 2, 3, 4, 5]
>>> lst1 is lst2
True
Required Questions
Nonlocal Codewriting
For the following question, write your code in lab06.py
.
Q1: Make Adder Increasing
Write a function which takes in an integer n
and returns a one-argument function.
This function should take in some value x
and return n + x
the first time it is called,
similar to make_adder
. The second time it is called, however, it should return n + x + 1
,
then n + x + 2
the third time, and so on.
def make_adder_inc(n):
"""
>>> adder1 = make_adder_inc(5)
>>> adder2 = make_adder_inc(6)
>>> adder1(2)
7
>>> adder1(2) # 5 + 2 + 1
8
>>> adder1(10) # 5 + 10 + 2
17
>>> [adder1(x) for x in [1, 2, 3]]
[9, 11, 13]
>>> adder2(5)
11
"""
"*** YOUR CODE HERE ***"
def adder(x):
nonlocal n
value = n + x
n = n + 1
return value
return adder
Use Ok to test your code:
python3 ok -q make_adder_inc
Map
Q2: Map
Write a function that takes a function and a list as inputs and maps the function on the given list - that is, it applies the function to every element of the list.
Be sure to mutate the original list. This function should not return anything.
def map(fn, lst):
"""Maps fn onto lst using mutation.
>>> original_list = [5, -1, 2, 0]
>>> map(lambda x: x * x, original_list)
>>> original_list
[25, 1, 4, 0]
"""
"*** YOUR CODE HERE ***"
# Iterative solution
for i in range(len(lst)):
lst[i] = fn(lst[i])
# Recursive solution
def map(fn, lst):
"""Maps fn onto lst using mutation.
>>> original_list = [5, -1, 2, 0]
>>> map(lambda x: x * x, original_list)
>>> original_list
[25, 1, 4, 0]
"""
if lst: # True when lst != []
temp = lst.pop(0)
map(fn, lst)
lst.insert(0, fn(temp))
Use Ok to test your code:
python3 ok -q map
Optional Questions
Q3: Nonlocal Environment Diagram
Draw the environment diagram that results from running the following code.
def moon(f):
sun = 0
moon = [sun]
def run(x):
nonlocal sun, moon
def sun(sun):
return [sun]
y = f(x)
moon.append(sun(y))
return moon[0] and moon[1]
return run
moon(lambda x: moon)(1)
After you've done it on your own, generate an environment diagram in python tutor to check your answer.