Let's suppose to have this simple code:
def my_outer_function():
outer_var = 123
def my_inner_function():
return outer_var + 1
return my_inner_function
get_inner = my_outer_function()
get_inner()
I wonder there isn't any kind of runtime error. outer_var
- the variable of the outer function - is available only when that function is running on, i.e. it vanishes when my_outer_function
ends. But when I call get_inner()
, my_outer_function()
is already ended so I would have bet on a runtime error since my_inner_function
couldn't find outer_var
.
How do you explain all this?