'How free variable value is determined within nested closure?
I have created a nested closure which looks as below:
def incrementer(n):
def inner(start):
current = start
def inc():
nonlocal current
current += n
return current
return inc
return inner
fn = incrementer(2)
Now, when I print value of co_freevars for fn I get below output:
print(fn.__code__.co_freevars) -> ('n',)
My understanding is that it should be ('current', 'n') because there are 2 free variables
here.
Why print(fn.__code__.co_freevars) is not printing ('current', 'n')?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
