I was learning about closures in Lua and came accross this code that works in Lua:
function newcounter()
local i = 0
function dummy()
i = i + 1
return i
return dummy
x = newcounter()
print(x())
-- this outputs 1, i.e., dummy uses the i variable from its enclosing function newcounter
I thought Python also supports a similar closure. So I tried the following code in Python3:
def nc():
i = 0
def dummy():
i = i + 1
return i
return dummy
x = nc()
print(x())
However, when I run the above Python code, I get the error that i
is accessed before being assigned!
Traceback (most recent call last):
File "/tmp/test.py", line 9, in <module>
print(x())
File "/tmp/test.py", line 4, in dummy
i = i + 1
UnboundLocalError: local variable 'i' referenced before assignment
Does this mean Python does not support closures? Or am I misunderstanding something about closures?