Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of eval in Python?

There is an eval() function in Python I stumbled upon while playing around. I cannot think of a case when this function is needed, except maybe as syntactic sugar. Can anyone give an example?

like image 497
ooboo Avatar asked Jul 06 '09 14:07

ooboo


People also ask

What is the use of eval?

You can use the Eval function to evaluate an expression that results in a text string or a numeric value. You can construct a string and then pass it to the Eval function as if the string were an actual expression. The Eval function evaluates the string expression and returns its value.

Is it good to use eval in Python?

The eval() function in Python can evaluate expressions dynamically. It could be very powerful and elegant sometimes. However, a powerful weapon is not easy to master. There will be security risks if we use the eval() function inappropriately.

What can I use instead of eval in Python?

literal_eval may be a safer alternative. literal_eval() would only evaluate literals, not algebraic expressions.

What is the difference between eval () and INT () function?

eval evaluates any python code. int tries to convert any type to integer (float, bool, string ...). you got it.


2 Answers

eval and exec are handy quick-and-dirty way to get some source code dynamically, maybe munge it a bit, and then execute it -- but they're hardly ever the best way, especially in production code as opposed to "quick-and-dirty" prototypes &c.

For example, if I had to deal with such dynamic Python sources, I'd reach for the ast module -- ast.literal_eval is MUCH safer than eval (you can call it directly on a string form of the expression, if it's a one-off and relies on simple constants only, or do node = ast.parse(source) first, then keep the node around, perhaps munge it with suitable visitors e.g. for variable lookup, then literal_eval the node) -- or, once having put the node in proper shape and vetted it for security issues, I could compile it (yielding a code object) and build a new function object out of that. Far less simple (except that ast.literal_eval is just as simple as eval for the simplest cases!) but safer and preferable in production-quality code.

For many tasks I've seen people (ab-)use exec and eval for, Python's powerful built-ins, such as getattr and setattr, indexing into globals(), &c, provide preferable and in fact often simpler solutions. For specific uses such as parsing JSON, library modules such as json are better (e.g. see SilentGhost's comment on tinnitus' answer to this very question). Etc, etc...

like image 153
Alex Martelli Avatar answered Sep 24 '22 13:09

Alex Martelli


The Wikipedia article on eval is pretty informative, and details various uses.

Some of the uses it suggests are:

  • Evaluating mathematical expressions
  • Compiler bootstrapping
  • Scripting (dynamic languages in general are very suitable to this)
  • Language tutors
like image 39
Noldorin Avatar answered Sep 24 '22 13:09

Noldorin