Use of eval in Python?
Solution 1
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...
Solution 2
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
Solution 3
You may want to use it to allow users to enter their own "scriptlets": small expressions (or even small functions), that can be used to customize the behavior of a complex system.
In that context, and if you do not have to care too much for the security implications (e.g. you have an educated userbase), then eval() may be a good choice.
Solution 4
In the past I have used eval() to add a debugging interface to my application. I created a telnet service which dropped you into the environment of the running application. Inputs were run through eval() so you can interactively run Python commands in the application.
Solution 5
In a program I once wrote, you had an input file where you could specify geometric parameters both as values and as python expressions of the previous values, eg:
a=10.0
b=5.0
c=math.log10(a/b)
A python parser read this input file and obtained the final data evaluating the values and the expressions using eval().
I don't claim it to be good programming, but I did not have to drive a nuclear reactor.
ooboo
Updated on July 18, 2022Comments
-
ooboo almost 2 years
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? -
ooboo almost 15 yearsI looked at this article and still don't understand... This is rather like a calculate function which calculates an arithmetic expression!
-
Noldorin almost 15 years@ooboo:
eval
can run any code in theory (hence the security risks pointed out in the article). -
Pete Kirkham almost 15 yearsI've done similar scripting the COM interface to Enterprise Architect to try out different queries
-
gahooa almost 15 yearsThis is not good. You should use the
__import__
builtin function, which is called by thefrom ... import ...
statement.