If I have an outer function returning an inner function like so:
def outer():
outer_var = 1
def inner():
return outer_var
outer_var = 2
return inner
Calling outer()()
will return 2. However, I want the inner function to return the value outer_var had when the inner function was created, not when it was called.
The only solution I could come up with was:
def outer():
outer_var = 1
def inner(inner_var=outer_var):
return inner_var
outer_var = 2
return inner
Calling outer()()
will now return 1. Is there a better solution that doesn't need kwargs? I don't think it's necessary to mention that in my actual code outer_var will only be known at runtime.