The y
in your lambda refers to the last value that y
had in the scope it came from, i.e., 9.
The easiest way to get the behavior you want is to use a default argument in your lambda:
lambda x, y=y: x/y
This captures the value of y
at the moment the lambda function is defined.
You can also do a "double-lambda", calling a function that returns the lambda you want, passing in the desired value of y
:
(lambda y: lambda x: x/y)(y)
Here, the outer lambda provides a new scope each time you call it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…