Does Python maintain function environment after a function is called to implement closure? -
i'm studying materials in cs61a,but 'withdraw' example in "2.4.4 local state" gives me illusion python maintains function environment after it's call finished in order implement closure.but it's seems costly. mechanism python takes implement closure.
when define outer functions returns inner function:
def outer(): x = 40 def inner(): return x + 2 return inner
you have access scope of outer function:
>>> func = outer() >>> func() 42
the value x
stored in tuple __closure__
:
>>> func.__closure__[0].cell_contents 40
Comments
Post a Comment