I am trying to understand how closure works in Python, and I've found the following code snippet:
def closure():
count = 0
def inner():
nonlocal count
count += 1
print(count)
return inner
start = closure()
start() # prints 1
start() # prints 2
start() # prints 3
I can understand this code, as when inner
function is defined, the enclosing scope has a variable named count with value 0, and the inner function will remember
this value then
However, if I then move count = 0
below inner function, so the code becomes like:
def closure():
def inner():
nonlocal count
count += 1
print(count)
count = 0
return inner
start = closure()
start() # prints 1
start() # prints 2
start() # prints 3
To my surprise, the code still works perfectly fine, which really confuses me. As when inner
is defined, the variable count
doesn't exist in the enclosing scope, how could inner
function is able to remember a value that doesn't exist in the namespace at this point yet?
Is it because something similar to JS's variable hoisting also exist in Python?