I believe this is a similar case, however, I'm not sure how to apply the solutions to my case.
I am creating a function within a loop that is supplied by an argument. The function is called later, but it is stored for later and is supplied with the argument.
for type_str in ["type_1", "type_2", "type_3"]:
@decorator_exec.register(...)
def to_exec(num: int):
print(type_str, num)
# later...
# this is an example
i = 0
for func in decorator_exec.funcs:
func(i)
i++
Unfortunately, this is what would be produced:
type_3 0
type_3 1
type_3 2
I would like the output to ensure that the type_str
variable (and any other variables that are included in to_exec()
body), be what it should be when it is defined.
type_1 0
type_2 1
type_3 2
The to_exec()
that is within the loop isn't used later within the loop, or outside the loop. It is solely stored by the decorator code and is called from its stored functions later.
If it isn't an XY problem, a possible solution would be to use a function factory (I'm not sure how that would work...)