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?
10 Answers
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
-
4
-
9@coelhudo Because you can modify nonlocal variables. But you cannot do assignment to nonlocal variables. E.g., this will raise UnboundLocalError:
def inner(): print d; d = {'y': 1}
. Here,print d
reads outerd
thus creating nonlocal variabled
in inner scope. May 1, 2012 at 17:52 -
21Thanks for this answer. I think you could improve the terminology though: instead of "can read, cannot change", maybe "can reference, cannot assign to". You can change the contents of an object in a nonlocal scope, but you can't change which object is referred to.– metamattFeb 4, 2013 at 6:46
-
3Alternatively, you can use arbitrary class and instantiate object instead of the dictionary. I find it much more elegant and readable since it does not flood the code with literals (dictionary keys), plus you can make use of methods. Aug 15, 2013 at 13:19
-
6For Python I think it is best to use the verb "bind" or "rebind" and to use the noun "name" rather than "variable". In Python 2.x, you can close over an object but you can't rebind the name in the original scope. In a language like C, declaring a variable reserves some storage (either static storage or temporary on the stack). In Python, the expression
X = 1
simply binds the nameX
with a particular object (anint
with the value1
).X = 1; Y = X
binds two names to the same exact object. Anyway, some objects are mutable and you can change their values.– stevehaDec 20, 2013 at 7:08
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)
-
It has not to be uggly. Instead of using inner.y, use outer.y. You can define outer.y = 0 before the def inner.– jgomo3Dec 27, 2013 at 14:33
-
1Ok, I see my comment is wrong. Elias Zamaria implemented the outer.y solution also. But as is commented by Nathaniel[1], you should beware. I think this answer should be promoted as the solution, but note about the outer.y solution and cavets. [1] stackoverflow.com/questions/3190706/…– jgomo3Dec 27, 2013 at 14:44
-
This gets ugly if you want more than one inner function sharing nonlocal mutable state. Say a
inc()
and adec()
returned from outer that increment and decrement a shared counter. Then you have to decide which function to attach the current counter value to and reference that function from the other one(s). Which looks somewhat strange and asymmetrical. E.g. indec()
a line likeinc.value -= 1
. Sep 14, 2016 at 8:03
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
-
Nice! This is indeed more elegant and readable than the dictionary.– VincenzoDec 15, 2015 at 19:17
-
I would recommend using slots in general here. It protects you from typos.– DerWehSep 1, 2018 at 10:43
-
@DerWeh interesting, I've never heard of that. Could you say the same of any class? I do hate getting flummoxed by typos. Sep 1, 2018 at 15:19
-
@BobStein Sorry, I don't really understand what you mean. But by adding
__slots__ = ()
and creating an object instead of using the class, e.g.context.z = 3
would raise anAttributeError
. It is possible for all classes, unless they inherit from a class not defining slots.– DerWehSep 2, 2018 at 17:13 -
@DerWeh I never heard of using slots to catch typos. You're right it would only help in class instances, not class variables. Maybe you would like to create a working example on pyfiddle. It would require at least two additional lines. Can't picture how you would do it without more clutter. Sep 2, 2018 at 21:20
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
-
Instead of creating a class and instantiating it, one can simply create a function:
def ns(): pass
followed byns.x = 3
. It ain't pretty, but it's slightly less ugly to my eye. Jan 25, 2011 at 11:36 -
2Any particular reason for the downvote? I admit mine is not the most elegant solution, but it works...– ig0774Mar 13, 2013 at 12:25
-
2
-
3I don't think this way simulates nonlocal, since it creates a global reference instead of a reference in a closure.– CarmeloSNov 24, 2014 at 2:43
-
1I agree with @CarmeloS, your last block of code in not doing what PEP-3104 suggests as way to accomplish something similar in Python 2.
ns
is a global object which is why you can referencens.x
at the module level in theprint
statement at the very end. Mar 28, 2015 at 15:14
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.
-
21Please beware: when implemented this way, if you do
f = outer()
and then later dog = outer()
, thenf
's counter will be reset. This is because they both share a singleouter.y
variable, rather than each having their own independent one. Although this code looks more aesthetically pleasing than Chris B's answer, his way seems to be the only way to emulate lexical scoping if you want to callouter
more than once.– N. VirgoMar 14, 2013 at 3:57 -
@Nathaniel: Let me see if I understand this correctly. The assignment to
outer.y
does not involve anything local to the function call (instance)outer()
, but assigns to an attribute of the function object that is bound to the nameouter
in its enclosing scope. And therefore one could equally well have used, in writingouter.y
, any other name instead ofouter
, provided it is known to be bound in that scope. Is this correct? Apr 16, 2013 at 8:13 -
1Correction, I should have said, after "bound in that scope": to an object whose type allows setting attributes (like a function, or any class instance). Also, since this scope is actually further out than the one we want, wouldn't this suggest the following extremely ugly solution: instead of
outer.y
use the nameinner.y
(sinceinner
is bound inside the callouter()
, which is exactly the scope we want), but putting the initialisationinner.y = 0
after the definition of inner (as the object must exist when its attribute is created), but of course beforereturn inner
? Apr 16, 2013 at 8:37 -
@MarcvanLeeuwen Looks like your comment inspired the solution with inner.y by Elias. Good thought by you. Jul 20, 2015 at 5:21
-
Accessing and updating global variables is probably not what most people are trying to do when mutating a closure’s captures.– binkiDec 16, 2015 at 7:05
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)
-
This one seems to be the best in terms of readability and preserving lexical scoping. Oct 8, 2014 at 3:17
-
Your second solution doesn't work if you call
outer()
more than once, as all the calls share the same counter. It's essentially no different from using a global variable.– interjayApr 6, 2021 at 17:48 -
@interjay: True, because function attributes are effectively global variables (as functions are themselves). To fix it would require a way to reset the value of function attributes — which is feasible. Apr 6, 2021 at 18:18
-
Resetting the value is still not good enough because it would prevent you from using two counters in parallel. I would just delete this part of the answer.– interjayApr 6, 2021 at 20:19
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.
-
Both versions work in this particular case but are not a replacement for
nonlocal
.locals()
creates a dictionary ofouter()
s locals at the timeinner()
is defined but changing that dictionary does not change thev
inouter()
. This won't work any more when you have more inner functions which want to share a closed over variable. Say ainc()
anddec()
that increment and decrement a shared counter. Sep 14, 2016 at 7:53 -
-
Yes I know. The question was how to achieve the effect of Python 3's
nonlocal
in Python 2 in general. Your ideas don't cover the general case but just the one with one inner function. Have a look at this gist for an example. Both inner functions have their own container. You need a mutable object in the scope of the outer function, as other answers suggested already. Sep 14, 2016 at 15:31 -
-
Yes that is the question. Take a look at the top of the page. adinsa has Python 2.6 and needs access to nonlocal variables in a closure without the
nonlocal
keyword introduced in Python 3. Sep 14, 2016 at 21:20
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
-
1actually I prefer the solution using a dictionary, but this one is cool :) Jan 10, 2014 at 16:39
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)
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.
-
It is much better to use a dictionary. You can reference the instance in
inner
, but can't assign to it, but you can modify it's keys and values. This avoids use of global variables. Feb 7, 2014 at 22:43