Python Eval(): Evaluate Expressions Dynamically - Real Python
Có thể bạn quan tâm
Table of Contents
- Understanding Python’s eval()
- The First Argument: expression
- The Second Argument: globals
- The Third Argument: locals
- Evaluating Expressions With Python’s eval()
- Boolean Expressions
- Math Expressions
- General-Purpose Expressions
- Minimizing the Security Issues of eval()
- Restricting globals and locals
- Restricting the Use of Built-In Names
- Restricting Names in the Input
- Restricting the Input to Only Literals
- Using Python’s eval() With input()
- Building a Math Expressions Evaluator
- Conclusion
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Evaluate Expressions Dynamically With Python eval()
Python’s eval() allows you to evaluate arbitrary Python expressions from a string-based or compiled-code-based input. This function can be handy when you’re trying to dynamically evaluate Python expressions from any input that comes as a string or a compiled code object.
Although Python’s eval() is an incredibly useful tool, the function has some important security implications that you should consider before using it. In this tutorial, you’ll learn how eval() works and how to use it safely and effectively in your Python programs.
In this tutorial, you’ll learn:
- How Python’s eval() works
- How to use eval() to dynamically evaluate arbitrary string-based or compiled-code-based input
- How eval() can make your code insecure and how to minimize the associated security risks
Additionally, you’ll learn how to use Python’s eval() to code an application that interactively evaluates math expressions. With this example, you’ll apply everything you’ve learned about eval() to a real-world problem. If you want to get the code for this application, then you can click on the box below:
Download the sample code: Click here to get the code you’ll use to learn about Python’s eval() in this tutorial.
Understanding Python’s eval()
You can use the built-in Python eval() to dynamically evaluate expressions from a string-based or compiled-code-based input. If you pass in a string to eval(), then the function parses it, compiles it to bytecode, and evaluates it as a Python expression. But if you call eval() with a compiled code object, then the function performs just the evaluation step, which is quite convenient if you call eval() several times with the same input.
The signature of Python’s eval() is defined as follows:
Python eval(expression[, globals[, locals]])Từ khóa » Hàm Eval
-
Hàm Eval() Trong Python.
-
Hàm Eval - Microsoft Support
-
Ví Dụ Và Công Dụng Của Hàm Eval Trong Python - T3H
-
Phương Thức Eval Trong Javascript - Freetuts
-
Eval() - JavaScript - MDN Web Docs - Mozilla
-
Hàm Eval Trong JavaScript
-
Python Eval() - Programiz
-
Hàm Eval() Trong Python. - Vay Tiền Online Bằng CMND
-
JavaScript Eval() Method - W3Schools
-
Python: Python Eval(): Đánh Giá Biểu Thức động | V1Study
-
Bai18: Hàm Eval() Tự động Tính Toán 1 Chuỗi - Tự Học Lập Trình Python
-
Python Eval() Function - W3Schools
-
Chuyển Công Thức ở Chuỗi Text Thành Hàm Trong Excel
-
Hàm Eval () Trong PHP - Plugin Thanh Toán