How come when I type the following
eval("mult = lambda x,y: (x*y)")
I get this as an error? What's going on?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
mult = lambda x,y: (x*y)
^
SyntaxError: invalid syntax
What am I doing wrong? If I enter the expression as is (no eval
) I get no error, and can use mult
to my hearts content.
You want to use exec instead of eval. I don't know why you would want to do this though when you can just use mult = lambda x,y : (x*y)
>>> exec("mult = lambda x,y : (x*y)")
>>> mult
<function <lambda> at 0x1004ac1b8>
>>> mult(3,6)
18
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With