Lab 1 - Closures and Scope

Instructions: Open a new word document and document your work as you complete each task and answer each question.

In this lab, you will use codelens to investigate the concepts of a closure and the scope of a variable. You will be given a series of complicated lambda expressions and asked to use codelens to answer some associated questions.

Closures

When a function is that references a value outside its scope, a closure is created. Recall that a closure is a function bundled with references to each of the variables defined outside the function definition.

Referencing global variable

Question 1

How does changing a global variable affect a function that references this variable?

A global variable is a variable that is defined at the top level, i.e. at the interpreter prompt. Global variables differ from local variables, which are defined inside the scope of a function, which includes the formal parameters and any variable defined inside the function.

Consider the following example.

In [1]: x = 3

In [2]: f = lambda y: x**y

In [3]: f(2)
Out[3]: 9

# Now change the value of x
In [4]: x = 4

# Does this change our answer?
In [5]: f(2)
Out[5]: 16

Use codelens to step through this example and then complete Task 1.

(lab1_1)

Task 1

Where the two function calls the same or different? Use what you learned from codelens to explain your answer.

Referencing local variables

Question 2

How does changing a local variable affect a function that references this variable?

Now we will make a similar example, but this time letting x be a local variable defined outside of an inner lambda expression.

In [6]: g = lambda x: lambda y: x**y

# "set" x to 3 with a function call
In [7]: f1 = g(3)

In [8]: f1(2)
Out[8]: 9

# "set" x to 4 with a function call
In [9]: f2 = g(4)

In [10]: f2(2)
Out[10]: 16

# What happens when we call the first function now?
In [11]: f1(2)
Out[11]: 9

Again step through the example in codelens and complete the following task.

(lab1_2)

Question 2

In what way ways does the second example differ from the first?

The Scope of a Variable

Now we will explore the scope of a variable. It is important to note that variables reference values. Remember that the scope of a variable is all the parts of the code where a variable will return it associated value.

Consider each of the following examples.

Example 1

In [12]: g = lambda x: lambda x: lambda x: 2*x

In [13]: f = g(1)

In [14]: h = f(2)

In [15]: h(3)
Out[15]: 6

(lab1_3)

Example 2

In [16]: g = lambda x: lambda y: lambda x: (x,y)

In [17]: f = g(1)

In [18]: h = f(2)

In [19]: h(3)
Out[19]: (3, 2)

(lab1_4)

Example 3

In [20]: g = lambda y: lambda x: lambda y: lambda x: (x,y)

In [21]: f = g(1)

In [22]: h = f(2)

In [23]: i = h(3)

In [24]: i(5)
Out[24]: (5, 3)

(lab1_5)

Question 3

For each of the following examples, describe the scope of each x and y. Hint: A diagram is a good approach.

Question 4

Rewrite each of the examples from the last task, this time renaming the variables using the capture free approach.

Note

If you are interested in the gritty details, the top two answers to this stackoverflow question include more details.

Next Section - Astronomy Animation