~/Building a Simple Python REPL
Mar 15, 2020
A REPL is a language shell that reads user input, evaluates it, prints the result, and loops back. In Python, you can create a basic REPL with a simple loop and the eval
function.
Here is an example:
This script will:
- Continuously read input from the user.
- Evaluate the input with
eval
. - Print the result.
- Quit if user enters
exit
orquit
.
For more complex statements, use exec
instead of eval
. For example:
Be cautious: using eval
or exec
can be dangerous if you run untrusted code. See the Python docs on eval for security guidelines.
This is the simplest way to build a Python REPL.