[lambda x: x * i for i in range(3)] produces three lambdas that ALL multiply by 2, because i is looked up when called, not when defined. Write make_multipliers(n) returning a list of n lambdas where the k-th correctly multiplies by k — fix the trap with a default argument.funcs = make_multipliers(3); [f(10) for f in funcs]
[0, 10, 20]
Each lambda captured its own k (0, 1, 2) instead of sharing the final value.
lambda x, k=k: x * k freezes k at definition time.
Without it, all closures share the loop variable.