Is it true that in Python the closure will persist if and only if it is mentioned by lexicaly in the return value
Let's say I have Python2 code
def foo(n):
bar = n
def dispatch(exp):
return eval(exp)
return dispatch
foo(10)("bar * 2") # throw NameError
It seems that the closure is bar
not being persisted. I am assuming it is not shown in the return function
On the other hand, if I mentioned bar
in the return function, although I did nothing, it is saved as a closure
def foo(n):
bar = n
def dispatch(exp):
if bar:
pass
return eval(exp)
return dispatch
foo(10)("bar * 2") # return 20
Did I understand correctly? This also applies to other languages that support closures, such as JavaScript, Scheme, ...
0
source to share
No one has answered this question yet
See similar questions:
or similar: