I'm trying to created nested functions in a loop in python, so each function would return a specific value depending on the value of said looped iterator.
Something like this:
for i in range(0, len(servers)):
f = lambda: print(servers[i]['servername'])
functions.append(f)
I would call the functions later on in the code an expect each function to return a given servername. But I always end up with all the functions returning the same thing (the last one) because they are all referring to the same thing, and not their local values.
Is what I'm trying to do at all possible? Or should I try this in another way?