Unexpected behaviour of lambda function?

Howdy Folks,

I am a little baffled:

2022-08-31_PythonFctList

I would have expected an output of 0…9 - not 10 times 9… Can someone explain me why this happens - I do not get it?

Thanks and Cheers
Dominik

OK, I obviously had some minutes of dumbness - now I think, I can answer it for myself - ‘indexI’ is not ‘replaced’ by the integer 0…9 but instead refers to the object containing an integer - said 9 - after the loop. This is due to the fact, that the lambda expression is not evaluated at the time of definition, but at the time of execution - right? Any ideas for a nice solution getting 0…9?

You’ve found your way into an issue common enough to be a FAQ :slight_smile:

Note: in future, please post code directly using a preformatted text “code block” (see the </> button in the edit toolbar) rather than posting a screenshot of code.

2 Likes

This seems to work

fctL = []
for indexI in range(10):
    fctL.append( eval("lambda x: %d" % indexI) )

for fct in fctL:
    print (fct(0))

A simple solution would be something like

fctL = []
for indexI in range(10):
    fctL.append(lambda x=indexI: x)

for fct in fctL:
    print(fct())

By using indexI as the default value for your lambda, you’re capturing its value at the time of creating your lambda, rather than capturing a reference to the variable itself (which will be modified), as in the original.

2 Likes