Strange behaviour of eval() with lambda functions inside a loop in python
问题 I have a string input file as below (one equation per line): 1.0 - x[0] - x[1] x[0] + x[1] I am trying to convert these equations to lambda functions using eval() in python and then use them in an optimization scheme. Here is what I do: def gen_const(input): list = input.read_eqs() for i in list: m = lambda x: eval(i) cons.extend([{'type': 'ineq', 'fun': m}]) return cons This cons fails when it is used inside the optimization scheme. However, if I only consider the first round inside the loop