I am new to programming and skim read some topics on functions and classes. So after reading about fucntions and enclosing functions, I tried to mimic inheritance search in classes by only minipulating functions and its scope.
Example:
For the Code
def f1():
t=1
def f2(x):
return eval(x)
return f2
Why do I get a name error when doing
f1()('t') #expecting 1 return
But not when defining say
def f1():
t=1
def f2():
return t
return f2
f()() # 1 is returned here
I could solve this problem by defining t as nonlocal in the scope of f2, which mean that the first code only looks in the local scope of f2. Why does this happen? `