A python codeblock l = [] for i in range(2): l.append(lambda: print(i)) for f in l: f() The result is 1 1
l = [] for i in range(2): l.append(lambda i=i: print(i)) for f in l: f() The Result is 0 1
Today I learned: Using variables in Python lambdas is weird
Apparently they store the variable names and not the values. Like a reference, even for primitives. But if you supply the value as a default argument, it works as intended.
#python #programming #programmerhumor #TIL