Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when using an assignment (of a lambda) in eval()?

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.

like image 623
tekknolagi Avatar asked Jun 16 '11 05:06

tekknolagi


1 Answers

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
like image 143
GWW Avatar answered Oct 09 '22 13:10

GWW