Python Closures and the Python 2.7 nonlocal Solution

by jasonyeo

In python, functions are first class. A language with first class functions allows functions to be passed as a variable and returned from a function just like other first class data types like integers or floats. Some notable language with first class functions are Python, JavaScript, Scheme and Haskell.

Therefore in python, it’s possible to do things like this:

def raise_to_base(n):
  def my_pow(x):
    return x**n
  return my_pow

>>> pow_base_5 = raise_to_base(5)
>>> pow_base_5(2) # => 32

(above code is taken from Python Closures Explained)

I have created a function that returns another function. And the inner function is known as a closure. A “closure” is an expression (typically a function) that can have free variables together with an environment that binds those variables (that “closes” the expression). With closures, we can do other neat things like this:

def outer():
  y = 0
  def inner():
    nonlocal y
    y+=1
    return y
  return inner


>>> f = outer()

>>> f()
1
>>> f()
2
>>> f()
3

The inner function now becomes something like a method in OOP. It controls the way the variable y is accessed from the outside. But there’s a problem when we want to do this in Python 2.7. The above code works only in Python 3.x. The nonlocal keyword does not exist in Python 2.7. To solve this problem, we can use dictionaries (or we can also create another object) to store the y variable in a namespace that the inner function can access.

def outer():
  d = {'y' : 0}
  def inner():
    d['y'] += 1
    return d['y']
  return inner

Though IMO, it’s kinda weird that we can’t access the non local variable but we can access the dictionary.

Advertisements