Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python, closures are read-only affairs; that is, a function defined inside another lexical scope cannot change variables outside of its local scope. Can someone explain why that is? There have been situations in which I would like to create a closure that modifies variables in the outer scope, but it wasn't possible. I realize that in almost all cases (if not all of them), this behavior can be achieved with classes, but it is often not as clean or as elegant. Why can't I do it with a closure?
Here is an example of a read/write closure:
def counter():
count = 0
def c():
count += 1
return count
return c
This is the current behavior when you call it:
>>> c()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in c
UnboundLocalError: local variable 'count' referenced before assignment
What I'd like it to do instead is this:
>>> c()
1
>>> c()
2
>>> c()
3