I'm trying to understand scope here a bit better.
def f1():
a = 1
def g1(X):
return X+a
return g1
def f2():
a = 1
def g2(X):
return X+1
return g2
g1 = f1()
print g1(4)
g2 = f2()
print g2(4)
# both give 5 as you'd expect
My problem is that isn't a
destroyed? In which scope is it available? My general understanding was that in the second case of f2
a is definitely not available once the function returns. This way for example if you have giant arrays or variables in terms of memory once the function returns they're no more.
What happens here?
EDIT:
Is b ever available here?
def f1():
a = 1
b = 1
def g1(X):
return X+a
return g1