What s the difference between eval exec and compile in Python

+1 vote

I've been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement.

Can someone please explain the difference between eval and exec, and how the different modes of compile() fit in?

Aug 28, 2018 in Python by bug_seeker
• 15,510 points
5,069 views

1 answer to this question.

0 votes
  1. exec is not an expression: a statement in Python 2.x, and a function in Python 3.x. It compiles and immediately evaluates a statement or set of statement contained in a string. Example:

  2. exec('print(5)') # prints 5.
    # exec 'print 5' if you use Python 2.x, nor the exec neither the print is a function there
    exec('print(5)\nprint(6)') # prints 5{newline}6.
    exec('if True: print(6)') # prints 6.
    exec('5') # does nothing and returns nothing.

  3. eval is a built-in function (not a statement), which evaluates an expression and returns the value that expression produces. Example:

  4. x = eval('5') # x <- 5
    x = eval('%d + 6' % x) # x <- 11
    x = eval('abs(%d)' % -100) # x <- 100
    x = eval('x = 5') # INVALID; assignment is not an expression.
    x = eval('if 1: x = 4') # INVALID; if is a statement, not an expression.

  5. compile is a lower level version of exec and eval. It does not execute or evaluate your statements or expressions, but returns a code object that can do it. The modes are as follows:

    1. compile(string, '', 'eval') returns the code object that would have been executed had you done eval(string). Note that you cannot use statements in this mode; only a (single) expression is valid.

    2. compile(string, '', 'exec') returns the code object that would have been executed had you done exec(string). You can use any number of statements here.

compile(string, '', 'single') is like the exec mode, but it will ignore everything except for the first statement. Note that an if/else statement with its results is considered a single statement.

answered Aug 28, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
2 answers

What's the difference between %s and %d in Python string formatting?

The reason is that they are using ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
22,795 views
0 votes
1 answer

What's the difference in Qt between setVisible, setShown and show/hide

show() is just a convenience function for ...READ MORE

answered Apr 17, 2018 in Python by anonymous
8,012 views
0 votes
5 answers
+2 votes
2 answers

What is the difference between print and return in python?

Return statements end the execution of a ...READ MORE

answered Aug 26, 2019 in Python by anonymous
8,045 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,443 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,020 points
2,400 views
0 votes
1 answer

What is the difference between Python and IPython?

There are few differences between Python and ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 58,020 points
4,008 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP