2

I think the code should be pretty self explanatory for what I'm trying to do.

functions = [lambda x:slope*x for slope in [1,2]]
functions

However this happens....

functions[0](1)
[output] 2

When it should have been one.

functions[1](1)
[output] 2

which is correct.

Why is it storing the same lambda function in both indices?

Thanks to linking a previous question in a comment, found my answer.

lambda x,slope=slope:slope*x for slope in [1,2]

The associated question was not sufficient to answer my question although it probably would be if I understood python better.

financial_physician
  • 1,222
  • 1
  • 11
  • 23

0 Answers0