0

Say I have the following string

a = "x**2+5"

and I want to use it to create the following lambda function:

b= lambda x: x**2+5

How do I do so?

1 Answer 1

4

You can use eval(): b = lambda x: eval(a). Just remember that using eval() in a real program, where you do not have control over a and x, may cause tons of problems.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.