What Is The Differences Between Python Eval And Exec Function

I use eval() and exec() function in recent python development, and i am not sure which function to use in which scenario, so i investigate python’s document and write down how to use them and the difference between eval() and exec() python function.

1. Eval Function.

1.1 eval function description.

This function is used to calculate the value of the specified expression.The Python code it executes can only be a single expression, not complex logic code, which is similar to lambda expression.

1.2 eval function definition.

eval(expression, globals=None, locals=None)
  1. expression : Required, it’s value can be either a string or an instance of a code object (which can be created by the compile function). If it is a string, it is parsed and interpreted as a Python expression. And the expression will use the globals and locals parameters value as global and local namespace.
  2. globals : Optional, it represent the global namespace (holding global variables) if supplied, must be a dictionary object.
  3. locals : Optional, represent the current local namespace (holding local variables) if supplied, can be any mapping object. If this parameter is ignored, it will take the same value as globals.
  4. If both globals and locals are ignored, their value will be local and global namespaces within the context where the eval() function is called.

1.3 eval function return value.

  1. If expression parameter is a code object, and the compile function’s mode parameter is ‘exec’ when the code object is created, then eval() function returns None.
  2. If expression parameter is an output statement, like print(), eval() returns None.
  3. If expression parameter is just an expression, then eval() function returns the expression caculated result value.

1.4 eval function example.

:~$ python3
Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
# declare global variable x, y and assign integer value.
>>> x = 10
>>> y = 20
# evaluate x + y expression, and return a.
>>> a = eval('x + y')
>>> print('a: ', a)
a:  30
>>> 
# evaluate x + y expression, but use globals parameter to override global x(10), y(20) value to x(1), y(2)
>>> b = eval('x + y', {'x': 1, 'y': 2})
>>> print('b: ', b)
b:  3
>>> 
# evaluate x + y expression, but set global x value to 1, and y is used a local variable y's value 3.
>>> c = eval('x + y', {'x': 1, 'y': 2}, {'y': 3, 'z': 4})
>>> print('c: ', c)
c:  4
>>> 
# execute output function print and return None.
>>> d = eval('print(x, y)')
10 20
>>> print('d: ', d)
d:  None

2. Exec Function.

2.1 exec function description.

Execute Python code dynamically. That is to say exec can execute complex Python code, unlike the eval function which can only evaluate an expression.

2.2 exec function definition.

exec(object[, globals[, locals]])
  1. object : Required, it is the Python code that need to be executed. It’s value has to be a string or a code object. If object is a string, the string is parsed into a set of Python statements and then executed (unless a syntax error occurs). If object is a code object, it is simply executed.
  2. globals, locals : Same as eval function.

2.3 exec function return value.

The return value of the exec function is always None. In Python 2, exec is not a function, but a built-in statement, but Python 2 has a function execfile().

3. Difference Between eval & exec Functions.

  1. eval() function can only evaluate a single expression, exec() function can run code snippets dynamically.
  2. eval() function can has return value, exec() function always return None.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.