You can use Python’s
eval() to evaluate Python expressions from a string-based or code-based input. This built-in function can be useful when you’re trying to evaluate Python expressions on the fly and you want to avoid the hassle of creating your own expressions evaluator from scratch.
In this course, you’ve learned how
eval() works and how to use it safely and effectively to evaluate arbitrary Python expressions.
You’re now able to:
- Use Python’s
eval()to dynamically evaluate basic Python expressions
- Run more complex statements like function calls, object creation, and attribute access using
- Minimize the security risks associated with the use of Python’s
Here are additional resources about eval(), compile(), and exec():
- eval(): Python Docs
- compile(): Python Docs
- exec(): Real Python tutorial
- ast.literal_eval(): Python Docs
- Eval Really is Dangerous: Ned Batchelder
- C-API: Python Docs
- Abstract Syntax Tree: Deeper Dive on Dynamic Code and Parsing Python
- AST Tokens: Companion Library to Abstract Syntax Tree
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.