Python --Difference between exec and eval

Introduction

Have you ever encountered an error using the exec or eval functions? In my case, I got caught quite often. In this article, I will explain the exec and eval functions with brief explanations and examples.

What is the exec function?

The exec function is a function that executes a statement. For example

sample.py


exec("x=5")
print(x)

#Execution result: 5

In this example, the exec function executes the statement of assigning 5 to x.

sample.py


x = exec("5+5")
print(x)

#Execution result: None

Since the exec function is a function that executes a statement, passing an expression does not return the execution result.

What is the eval function?

The eval function is a function that calculates an expression and returns the result. For example

sample.py


x = eval("5+5")
print(x)

#Execution result: 10

The exec function did not return the calculation result when I passed the expression, but the eval function did.

sample.py


eval("x=5")
print(x)

#SyntaxError: invalid syntax in line 1

Since the eval function is a function that calculates an expression, passing a statement will cause a SyntaxError.

Finally

Have you read this article and found out about the differences between the exec and eval functions? If you have any comments, please write them in the comments.

Recommended Posts

Python --Difference between exec and eval
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between == and is in python
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
difference between statements (statements) and expressions (expressions) in Python
Difference between PHP and Python finally and exit
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
[python] Difference between variables and self. Variables in class
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Difference between SQLAlchemy filter () and filter_by ()
exec, eval to execute [python] statement
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Memorandum (difference between csv.reader and csv.dictreader)
List concatenation method in python, difference between list.extend () and β€œ+” operator
(Note) Difference between gateway and default gateway
Cooperation between python module and API
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between sort and sorted (memorial)
Difference between SQLAlchemy flush () and commit ()
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
File open function in Python3 (difference between open and codecs.open and speed comparison)
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
[Xg boost] Difference between softmax and softprob
[Django ORM] Difference between values () and only ()
Extraction of tweet.js (json.loads and eval) (Python)
Differences in syntax between Python and Java
Difference between linear regression, Ridge regression and Lasso regression
Difference between docker-compose env_file and .env file
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
speed difference between wsgi, Bottle and Flask
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between numpy.ndarray and list (dimension, size)
Correspondence between Python built-in functions and Rust
Difference between ls -l and cat command
Exchange encrypted data between Python and C #
Difference and compatibility verification between keras and tf.keras # 1
[Introduction to Python] What is the difference between a list and a tuple?