I saved lambda functions in an array, but when i tried to call for example the first or second function, always the last one added to the array is called. But when I call them in a for-loop they are called correctly. How can I call the functions correctly using the position? And why does this happen?
The following code is how I tried to isolate the problem.
pos=2
always returns ten times 9
(always calls the last appended function) and pos=1
returns always 0 to 9 (how I would want it to be).
l=[i for i in range(10)]
#the list of the functions
funcs=[]
#the used call-possibility
pos= #1/2
#initialising of the functions
for i in l:
funcs.append(lambda:print(i))
#possibilities of calling the functions
if pos==1:
for i in range(len(l)):
print(l[i])
funcs[i]()
if pos==2:
x=0
while x<10:
funcs[x]()
x+=1