What do lambda function closures capture?

you may force the capture of a variable using an argument with a default value:

>>> for i in [0,1,2,3]:
...    adders[i]=lambda a,i=i: i+a  # note the dummy parameter with a default value
>>> print( adders[1](3) )

the idea is to declare a parameter (cleverly named i) and give it a default value of the variable you want to capture (the value of i)

Leave a Comment