nonlocal keyword in Python 2.x

PythonClosuresPython 2.xPython Nonlocal

Python Problem Overview


I'm trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in closures in these versions of python?

Python Solutions


Solution 1 - Python

Inner functions can read nonlocal variables in 2.x, just not rebind them. This is annoying, but you can work around it. Just create a dictionary, and store your data as elements therein. Inner functions are not prohibited from mutating the objects that nonlocal variables refer to.

To use the example from Wikipedia:

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

f = outer()
print(f(), f(), f()) #prints 1 2 3

Solution 2 - Python

The following solution is inspired by the answer by Elias Zamaria, but contrary to that answer does handle multiple calls of the outer function correctly. The "variable" inner.y is local to the current call of outer. Only it isn't a variable, since that is forbidden, but an object attribute (the object being the function inner itself). This is very ugly (note that the attribute can only be created after the inner function is defined) but seems effective.

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

f = outer()
g = outer()
print(f(), f(), g(), f(), g()) #prints (1, 2, 1, 3, 2)

Solution 3 - Python

Rather than a dictionary, there's less clutter to a nonlocal class. Modifying @ChrisB's example:

def outer():
    class context:
        y = 0
    def inner():
        context.y += 1
        return context.y
    return inner

Then

f = outer()
assert f() == 1
assert f() == 2
assert f() == 3
assert f() == 4

Each outer() call creates a new and distinct class called context (not merely a new instance). So it avoids @Nathaniel's beware about shared context.

g = outer()
assert g() == 1
assert g() == 2

assert f() == 5

Solution 4 - Python

I think the key here is what you mean by "access". There should be no issue with reading a variable outside of the closure scope, e.g.,

x = 3
def outer():
    def inner():
        print x
    inner()
outer()

should work as expected (printing 3). However, overriding the value of x does not work, e.g.,

x = 3
def outer():
    def inner():
        x = 5
    inner()
outer()
print x

will still print 3. From my understanding of PEP-3104 this is what the nonlocal keyword is meant to cover. As mentioned in the PEP, you can use a class to accomplish the same thing (kind of messy):

class Namespace(object): pass
ns = Namespace()
ns.x = 3
def outer():
    def inner():
        ns.x = 5
    inner()
outer()
print ns.x

Solution 5 - Python

There is another way to implement nonlocal variables in Python 2, in case any of the answers here are undesirable for whatever reason:

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

f = outer()
print(f(), f(), f()) #prints 1 2 3

It is redundant to use the name of the function in the assignment statement of the variable, but it looks simpler and cleaner to me than putting the variable in a dictionary. The value is remembered from one call to another, just like in Chris B.'s answer.

Solution 6 - Python

Here's something inspired by a suggestion Alois Mahdal made in a comment regarding another answer:

class Nonlocal(object):
    """ Helper to implement nonlocal names in Python 2.x """
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)


def outer():
    nl = Nonlocal(y=0)
    def inner():
        nl.y += 1
        return nl.y
    return inner

f = outer()
print(f(), f(), f()) # -> (1 2 3)

Solution 7 - Python

There is a wart in python's scoping rules - assignment makes a variable local to its immediately enclosing function scope. For a global variable, you would solve this with the global keyword.

The solution is to introduce an object which is shared between the two scopes, which contains mutable variables, but is itself referenced through a variable which is not assigned.

def outer(v):
    def inner(container = [v]):
        container[0] += 1
        return container[0]
    return inner

An alternative is some scopes hackery:

def outer(v):
    def inner(varname = 'v', scope = locals()):
        scope[varname] += 1
        return scope[varname]
    return inner

You might be able to figure out some trickery to get the name of the parameter to outer, and then pass it as varname, but without relying on the name outer you would like need to use a Y combinator.

Solution 8 - Python

Another way to do it (although it's too verbose):

import ctypes

def outer():
    y = 0
    def inner():
        ctypes.pythonapi.PyCell_Set(id(inner.func_closure[0]), id(y + 1))
        return y
    return inner

x = outer()
x()
>> 1
x()
>> 2
y = outer()
y()
>> 1
x()
>> 3

Solution 9 - Python

Extending Martineau elegant solution above to a practical and somewhat less elegant use case I get:

class nonlocals(object):
""" Helper to implement nonlocal names in Python 2.x.
Usage example:
def outer():
     nl = nonlocals( n=0, m=1 )
     def inner():
         nl.n += 1
     inner() # will increment nl.n

or...
    sums = nonlocals( { k:v for k,v in locals().iteritems() if k.startswith('tot_') } )
"""
def __init__(self, **kwargs):
    self.__dict__.update(kwargs)

def __init__(self, a_dict):
    self.__dict__.update(a_dict)

Solution 10 - Python

Use a global variable

def outer():
	global y # import1
	y = 0
	def inner():
		global y # import2 - requires import1
		y += 1
		return y
	return inner

f = outer()
print(f(), f(), f()) #prints 1 2 3

Personally, I do not like the global variables. But, my proposal is based on https://stackoverflow.com/a/19877437/1083704 answer

def report():
        class Rank: 
            def __init__(self):
                report.ranks += 1
        rank = Rank()
report.ranks = 0
report()

where user needs to declare a global variable ranks, every time you need to call the report. My improvement eliminates the need to initialize the function variables from the user.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionadinsaView Question on Stackoverflow
Solution 1 - PythonChris B.View Answer on Stackoverflow
Solution 2 - PythonMarc van LeeuwenView Answer on Stackoverflow
Solution 3 - PythonBob SteinView Answer on Stackoverflow
Solution 4 - Pythonig0774View Answer on Stackoverflow
Solution 5 - PythonElias ZamariaView Answer on Stackoverflow
Solution 6 - PythonmartineauView Answer on Stackoverflow
Solution 7 - PythonMarcinView Answer on Stackoverflow
Solution 8 - PythonEzer FernandesView Answer on Stackoverflow
Solution 9 - PythonAmnon HarelView Answer on Stackoverflow
Solution 10 - PythonValView Answer on Stackoverflow