Use of eval in Python?

Question:

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?

Asked By: ooboo

||

Answers:

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
Answered By: Noldorin

eval() is not normally very useful. One of the few things I have used it for (well, it was exec() actually, but it’s pretty similar) was allowing the user to script an application that I wrote in Python. If it were written in something like C++, I would have to embed a Python interpreter in the application.

Answered By: Zifre

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.

Answered By: Stefano Borini

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.

Answered By: Andre Miller

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.

Answered By: rob

Eval is a way to interact with the Python interpreter from within a program. You can pass literals to eval and it evaluates them as python expressions.

For example –

print eval("__import__('os').getcwd()")

would return the current working directory.

cheers

Answered By: Arnkrishn

I use it as a quick JSON parser …

r='''
{
    "glossary": {
        "title": "example glossary"
        }
}
'''

print eval(r)['glossary']['title']
Answered By: tinnitus

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…

Answered By: Alex Martelli

I use exec to create a system of plugins in Python.

    try:
        exec ("from " + plugin_name + " import Plugin")
        myplugin = Plugin(module_options, config=config)
    except ImportError, message:
        fatal ("No such module " + plugin_name + 
               " (or no Plugin constructor) in my Python path: " + str(message))
    except Exception:
        fatal ("Module " + plugin_name + " cannot be loaded: " + 
               str(sys.exc_type) + ": " + str(sys.exc_value) + 
               ".n    May be a missing or erroneous option?")

With a plugin like:

class Plugin:

    def __init__ (self):
        pass

    def query(self, arg):
         ...

You will be able to call it like:

    result = myplugin.query("something")

I do not think you can have plugins in Python without exec/eval.

Answered By: bortzmeyer

I used it to input variable values to the main program:

test.py var1=2 var2=True

var1=0
var2=False
for arg in sys.argv[1:]:
    exec(arg)

A crude way to allow keyword args in the main program. If there’s a better way let me know!

Answered By: buggy

eval() is for single sentence, while exec() is for multiple ones.

usually we use them to add or visit some scripts just like bash shell.

because of they can run some byte scripts in the memory, if you have some important data or script you can decode and unzip your ‘secret’ then do everything you wanna.

Answered By: BBDXF

I just came across a good use of eval. I was writing a test suite for some code, and created a Test class, where every method was a test to be run. I wanted a way so that I could run all the test methods without having to call each method individually. So, I wrote something rather dirty.

class Test:
    def __init__(self, *args):
       #bs

    def test1(self):
       #bs

    def test2(self):
       #bs

if __name__ == "__main__":
    import argparse
    #argparse bs
    test = Test(*bs_args)
    for func in (i for i in dir(test) if i[0] != '_' and i not in test.__dict__):
        print(eval('test.{func}()'.format(func = func)))

Dynamic evaluation of arbitrary test cases is pretty cool. I just have to write the method, and after saving I can include the method in my test suite. As for the code, I basically just inspect the methods defined in the test object, and make sure they aren’t default python “magic” methods or attributes to the Test object. After that I can assume they are methods and can be evaluated.

Answered By: Casey Craig

You can use eval in a decorator:

#this replaces the original printNumber with a lambda-function,
#which takes no arguments and which calls the old function with
#the number 10
@eval("lambda fun: lambda: fun(10)")
def printNumber(i: int) -> None:
    print("The number is %i", i)

#call
printNumber()

while you cannot use complex expressions like

@lambda fun: lambda: fun(10)
def ...

nor

@(lambda fun: lambda: fun(10))
def ...

You cannot use a lambda-expression there, because the decorator should either be an identifier:

@myModule.functionWithOneArg

or a function call:

@functionReturningFunctionWithOneArg(any, "args")

You see that the call of the function eval with a string has valid syntax here, but the lambda-expression not. (-> https://docs.python.org/3/reference/compound_stmts.html#function-definitions)

Answered By: cmdLP

I had a case where I used eval in combination with an informix database. For some reason the query returned a string formed like this

query_result = "['1', '2', '3']"

I just used eval on the query result so python interpreted it as a list of strings.

[int(i) for i in eval(query_result)]
> [1,2,3]

I could not change the db so this was a quick (and dirty) way to get the integers.

Answered By: N. Ahlers
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.