Difference between Eval() and Exec() functions in Python

This tutorial will give us a piece of extraordinary information about the use of EVAL ()  and  EXEC() functions in Python.

These are very useful functions for evaluating the simple expressions and the complex code in Python.

Eval() function in Python:

This function is using in python for evaluating the simple mathematical expressions.

And this will not support to evaluate the complex code(more than one line of code).

syntax: 

 eval(expression, global values=None,local values=None)

In the above syntax

1. Expression: In this session, we should write the target expression with the variables as a string or object code.

  •  And we can give direct values for evaluation in place of variables.
  •  Example:
    eval('x+y',{'x':3,'y':2})
    
    eval('5+6+1')
    output:5
           12

2. Global values: We can use global values in eval function or it is optional.

  • If no locals present in the function, the variables will be assigned to global values.
  • And global values must be declared as a dictionary.
  • Example:
     eval('x+y',{'x':1,'y':2},{'a':3,'b':4})
    output:3
  • From the above example, we observe that X and Y are global variables and ‘a’ and ‘b’ are locals.
  • The global values were assigned because of not presenting the locals with the names of variables present in an expression.

 

3. Local values: These values have the highest priority than the global values in the function.

  • If local and global values with the same variable names the variables will assign to the local values.
  •  And local values should declare as a dictionary.
  • And these are also optional.
  • Example:
    eval('a+b',{'a':2,'b':5},{'a':3,'b':4})
    
    output:7
  • Here both local and global values declared with the same names the variables take the local values.
  • Here local values are 3 and 4.
  • And global values are 2 and 5.

Example for eval() function :

r=eval('x+y',{'x':2,'y':4} ,{'y':5,'z':9})
print(r)
output:7

Explanation:

  • From the above example, ‘x ‘ and ‘y’ are global values.
  • And ‘y’ is also present in local values so the ‘y’ present in the expression will take the value as local value 5.
  • Because there is no ‘x’ present in locals the ‘x’ takes the value of ‘x’ present in global values.

Exec() function in Python:

This function used to execute the complex Python code dynamically, unlike the eval() function can only execute a single expression.

Syntax: exec(object[,global values[,locals]])

  • object: string or object code.
  • global values: this is must be a dictionary and optional.
  • locals: can be a mapping object (optional).

“Exec() function supports all the methods.”

For example:

program='print("sum of 5 and 6 is:",(5+6))'
exec(program)
output: sum of 5 and 6 is: 11

Global and Local parameters:

Global parameters: Example

from math import *
exec("print(fact(2))",{"fact":factorial})
output:2

Explanation:

Here “fact” is a key and factorial is the value assigned to the fact that is 2.

So, the output is  2*1=2.

And the dictionary should not empty if it is empty it shows output like “no output” and this is the rise of exception.

And { }(empty dictionary) represent all functions are restricted on the object.

Example:

from math import *
exec("print(fact(5))",{})#exception will rise

output: no output # this is the exception

Local parameters: Example

from math import *
exec("print(ceil(5.5))",{"fact":factorial},{"ceil":ceil})
output:6

Explanation:

So the ceil() function is defined in the dictionary as a local parameter and we got output as 6 for ceil(5.5),

and “fact” is defined in a dictionary as a global parameter.

Leave a Reply

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