I need to create a set of similar static functions in python. This involves variables outside the function that I want to use as hard-coded values when a function is created. However, functions created this way remain linked to variables and change dynamically:
def repeat_n():
res = []
for _ in range(n):
res.append(1)
return res
n = 3
repeat_three = repeat_n
repeat_three()
n =+ 1
# repeats four times
repeat_three()
repeat_three = repeat_ndoesn't create a new function. It's the same functionnwith the value ofnat the time of the copy.nis a free variable, whose value is looked up when you call the function.