I want to write a function which returns itself a function.
from types import FunctionType
import numpy as np
def outer_function(beta: np.array, K: np.array, increment: str,
augmented: bool, prior: FunctionType=None, **kwargs) -> FunctionType:
def g(x: float) -> float:
if prior is None:
prior = x**2
return beta+x+prior
return g
The problem I'm encountering is that certain variables of the outer function are not in the scope of the inner, while others are
test = outer_function(np.array(0), np.array([1,2,3]), increment="hello", augmented=False, prior=None, alpha=10)
In [91]: pdb.runcall(test, 10)
> <ipython-input-87-16d13c025c81>(5)g()
-> if prior is None:
(Pdb) print(prior)
*** NameError: name 'prior' is not defined
(Pdb) dir()
['beta', 'x']
(Pdb) print(x)
10
(Pdb)
as you can see I only get the variable x and beta. But all the other variables are not seen by g. How can I make all variables of the outer accessible for the inner?
UnboundLocalError: local variable 'prior' referenced before assignmentbefore you started debugging. If you had searched for that you would have seen answers on stackoverflow.