0

I want to define many Python functions that use variables from the outer scope, as shown in the following code:

functions = []
for i in A:
    for j in B:
       def c(x):
          return i*x+j*(x**2)
       functions.append(c)

However, I want the function to not change, once defined.

In the code above, all functions are exactly the same because, when called, they will read the current value of i and j. The will not use the value that i and j had when the function was defined.

How can I define a function using outer variables, in a way that the function doesn't change (i.e., only depends on the parameters), once defined?

0 Answers0