Python eval function dynamically evaluates mathematical expressions

王林
Release: 2023-04-11 23:43:18
forward
2066 people have browsed it

​In this article, Mr. Yun Duo will study with you from the following two aspects.

  • How Python’s eval() works
  • How to use eval() to dynamically calculate arbitrary string-based or compiled code-based input

Python eval function dynamically evaluates mathematical expressions

In addition, later posts will learn how to use Python’s ​eval()​ to code an application that interactively evaluates mathematical expressions. With this example, we'll apply everything we've learned about eval() to a real-world problem.

Python’s eval()

We can use the built-in Python eval()[1] to dynamically evaluate expressions from string-based or compiled code-based input. If we pass a string to eval()​, then the function parses it, compiles it to bytecode[2], and evaluates it as a Python expression. But if we call eval() with a compiled code object, then the function only performs the calculation step, which is very convenient if we call eval() multiple times with the same input.

Python's eval() is defined as follows.

eval(expression[, globals[, locals]])
Copy after login

This function requires a first parameter, called expression, which contains the expression that needs to be calculated. eval() also requires two optional parameters.

  1. globals
  2. locals

In the following content, we will learn what these parameters are and how eval() uses them to instantiate Evaluate Python expressions.

Note: We can also use exec()[3] to dynamically execute Python code. The main difference between eval()​ and exec()​ is that eval()​ can only execute or evaluate expressions, while exec() can execute any piece of Python code.

First parameter: expression

The first parameter of eval() is called expression. It is a required parameter that is used to save the string-based or compilation-based function. code input. When eval()​ is called, the contents of expression are evaluated as a Python expression. Below is an example using string-based input.

>>> eval("2 ** 8")
256
>>> eval("1024 + 1024")
2048
>>> eval("sum([8, 16, 32])")
56
>>> x = 100
>>> eval("x * 2")
200
Copy after login

When eval() is called with a string as a parameter, the function returns the result of the calculation on the input string. By default, eval()​ has access to global variable names, such as x in the example above.

To evaluate a string-based expression, Python's eval() runs the following steps.

  1. Parse the expression
  2. Compile it to bytecode
  3. Evaluate it as a Python expression
  4. Return the result of the evaluation

The first parameter expression of eval() emphasizes that this function only acts on expressions, not compound statements [4]. The Python documentation defines expression as follows.

expression

A syntax that can be evaluated to a value. In other words, an expression is an accumulation of expression elements, such as literals, names, property accesses, operators, or function calls, all of which return a value. In contrast to many other languages, not all language constructs are expressions. There are also some statements that cannot be used as expressions, such as while. In addition, assignment is also a statement, not an expression.

On the other hand, Python statement has the following definition.

statement

A statement is part of a suite (a "block" of code). statement is either an expression or one of several structures with keywords, such as if, while, or for.

If you pass a compound statement to eval()​, you will get a SyntaxError. The following example uses eval() to execute an if statement.

>>> x = 100
>>> eval("if x: print(x)")
File "<string>", line 1
if x: print(x)
^
SyntaxError: invalid syntax
Copy after login

The error reported above is because eval() only accepts expressions. Any other statements, such as if, for, while, import, def, or class, will raise an error.

Note: The for loop is a compound statement, but the for keyword can also be used in a derivation, in which case it is considered an expression. Derivations can be evaluated using eval()​, even if they use the for keyword.

eval() also does not allow assignment operations.

>>> eval("pi = 3.1416")
File "<string>", line 1
pi = 3.1416
 ^
SyntaxError: invalid syntax
Copy after login

If we pass an assignment operation as a parameter to eval()​, we will get a SyntaxError. Assignment operations are statements, not expressions, and statements are not allowed to be used with eval().

When the parser does not understand the input expression, you will also get a SyntaxError. The following example evaluates an expression that violates Python syntax.

>>> # Incomplete expression
>>> eval("5 + 7 *")
File "<string>", line 1
5 + 7 *
^
SyntaxError: unexpected EOF while parsing
Copy after login

所以,不能把一个违反 Python 语法的表达式传给 eval()​ 。在上面的例子中,我们尝试计算一个不完整的表达式 ("5 + 7 *") 时抛出一个 SyntaxError,因为分析器不理解表达式的语法。

我们也可以把已编译的代码对象传递给 eval()​ 。因此可以使用函数 compile()[7] ,一个内置函数,可以将输入的字符串编译成代码对象[8] 或 AST 对象[9],这样就可以用 eval() 来计算它。

如何使用compile()的细节超出了本文的范围,但这里可以快速了解一下它的前三个必要参数。

source保存我们要编译的源代码。这个参数可以接受普通字符串、字节字符串[10]和AST对象。

filename给出读取代码的文件。如果我们要使用一个基于字符串的输入,那么这个参数的值应该是""。

mode指定了我们想得到哪种编译后的代码。如果我们想用eval()​来处理编译后的代码,那么这个参数应该被设置为"eval"。

我们可以使用 compile()​ 向eval()提供代码对象,而不是普通的字符串。

>>> # 算术运算
>>> code = compile("5 + 4", "<string>", "eval")
>>> eval(code)
9
>>> code = compile("(5 + 7) * 2", "<string>", "eval")
>>> eval(code)
24
>>> import math
>>> # 一个球体的体积
>>> code = compile("4 / 3 * math.pi * math.pow(25, 3)", "<string>", "eval")
>>> eval(code)
65449.84694978735
Copy after login

如果我们使用 compile()​ 来编译要传递给eval()​的表达式,那么eval()会经过以下步骤。

  1. 计算编译后的代码
  2. 返回计算的结果

如果使用基于编译码的输入调用 eval()​ ,那么该函数会执行计算步骤并立即返回结果。当需要多次计算同一个表达式时,这可能很方便。在这种情况下,最好预先编译表达式,并在随后调用 eval() 时重复使用产生的字节码。

如果我们事先编译了输入表达式,那么连续调用eval()将运行得更快,因为我们不会重复解析和编译的步骤。如果我们正在计算复杂的表达式,不需要的重复会导致高的CPU时间和过度的内存消耗。

第二个参数:globals

eval()​ 的第二个参数 globals,可选的,字典类型,为 eval()​ 提供一个全局命名空间。通过 globals 告诉 eval() 在计算表达式时要使用哪些全局变量名。

全局变量名是所有那些在当前全局范围或命名空间中可用的变量名。可以从代码的任何地方访问它们。

在字典中传递给 globals 的所有名字在执行时都可以提供给 eval()​ 。请看下面的例子,它展示了如何使用一个自定义的字典来为 eval() 提供一个全局命名空间。

>>> x = 100# 一个全局变量
>>> eval("x + 100", {"x": x})
200
>>> y = 200# 另一个全局变量
>>> eval("x + y", {"x": x})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
Copy after login

如果为 eval()​ 的 globals 参数提供一个自定义字典,那么 eval()​ 将只接受这些名字作为 globals。在这个自定义字典之外定义的任何全局变量名都不能从 eval()​ 内部访问。这就是为什么当你试图在上述代码中访问 y 时,Python 会引发一个 NameError。传递给 globals 的字典不包括 y。

可以通过在字典中列出名字来插入 globals,然后这些名字在求值过程中就会出现。例如,如果在 globals 中插入了 y​,那么在上面的例子中对 "x + y" 的求值将如期进行。

>>> eval("x + y", {"x": x, "y": y})
300
Copy after login

因为把 y​ 添加到了自定义 globals 字典中,所以成功计算 "x + y" 的值,得到的预期返回值 300。

我们也可以提供不存在于当前全局范围的变量名。此时需要为每个名字提供一个具体的值。eval()在运行时将把这些变量名解释为全局变量名。

>>> eval("x + y + z", {"x": x, "y": y, "z": 300})
600
>>> z
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
Copy after login

尽管z​没有在当前的全局范围内定义,但是这个变量在全局中的值是300,此时eval()​可以访问z,就像它是一个全局变量一样。

globals 背后的机制是相当灵活的,可以向 globals 传递任何可见的变量(全局、局部、或者非局部)。还可以传递自定义的键值对,比如上面例子中的 "z": 300​,那么eval() 将把它们全部作为全局变量处理。

关于 globals 中的注意事项,如果我们提供给它的自定义字典不包含键值 "__builtins__​",那么在表达式被解析之前,对内置字典的引用将自动插入 "__builtins__​" 下面。这可以确保 eval() 在计算表达式时可以完全访问所有的 Python 内置变量名。

下面的例子表明,即使给 globals 提供了一个空的字典,对 eval() 的调用仍然可以访问 Python 的内置变量名。

>>> eval("sum([2, 2, 2])", {})
6
>>> eval("min([1, 2, 3])", {})
1
>>> eval("pow(10, 2)", {})
100
Copy after login

在上面的代码中,我们向 globals 提供了一个空的字典 ({}​)。由于这个字典不包含一个叫做 "__builtins__​" 的键,Python 会自动插入一个指向 builtins 中名字的引用。这样,eval() 在解析表达式时就可以完全访问所有 Python 的内置名字。

如果调用 eval()​ 而没有将自定义字典传递给 globals ,那么参数将默认为在调用 eval()​的环境中 globals() 返回的字典:

>>> x = 100#一个全局变量
>>> y = 200# 另一个全局变量
>>> eval("x + y")# 访问两个全局变量
300
Copy after login

当调用 eval()​ 而不提供 globals 参数时,该函数使用 globals()​ 返回的字典作为其全局命名空间来计算表达式。所以,在上面的例子中,我们可以自由地访问 x​ 和 y,因为它们是包含在我们当前全局范围内的全局变量。

第三个参数:locals

Python 的 eval()​ 第三个参数 locals ,可选参数,字典类型。此时这个字典包含了 eval() 在计算表达式时作为局部变量名使用的变量。

局部变量名是那些我们在一个给定的函数内定义的名称(变量、函数、类等等)。局部名称只在封闭的函数内可见。我们在编写函数时定义这些变量名。

因为 eval()​ 已经写好了,所以不能在它的代码或局部范围内添加局部变量名。然而可以向 locals​ 传递一个字典,eval()会把这些名字当作本地名字。

>>> eval("x + 100", {}, {"x": 100})
200
>>> eval("x + y", {}, {"x": 100})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
Copy after login

第一个调用 eval()​ 的第二个字典保存了变量 x​。这个变量被 eval()​ 解释为一个局部变量。换句话说,它被看作是在 eval() 中定义的一个变量。

我们可以在表达式中使用 x​,并且 eval()​ 可以访问它。相反,如果使用y​,那么会得到一个 NameError,因为y没有定义在 globals 命名空间或 locals 命名空间。

和 globals 一样,可以向 locals 传递任何可见的变量(全局、局部或非局部)。也可以传递自定义的键值对,比如 "x"​。eval()将把它们全部作为局部变量处理。

注意,要给 locals 提供一个字典,首先需要给 globals 提供一个字典。不能在 eval() 中使用关键字参数。

>>> eval("x + 100", locals={"x": 100})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: eval() takes no keyword arguments
Copy after login

如果在调用 eval()​ 时使用关键字参数,那么抛出一个 TypeError。这是因为 eval() 不接受关键字参数,所以在提供 locals 字典之前,需要先提供一个 globals 字典。

如果没有给 locals 传递一个字典,那么它就默认为传递给 globals 的字典。这里有一个例子,给 globals 传递了一个空的字典,而 locals 没有传递任何值。

>>> x = 100
>>> eval("x + 100", {})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'x' is not defined
Copy after login

鉴于没有给 locals 提供一个自定义的字典,这个参数默认为传递给 globals 的字典。此时eval()​ 无法访问 x,因为 globals 持有一个空的字典。

globals 和 locals 之间的主要实际区别是,如果"__builtins__​"键不存在,Python 会自动插入 globals 中。无论我们是否为 globals 提供了一个自定义的字典,这都会发生。此外,如果我们给 locals 提供了一个自定义的字典,那么在执行 eval() 的过程中,这个字典将保持不变。

用 eval() 计算表达式

我们可以使用Python的eval()来计算任何一种Python表达式,但不包括Python语句,如基于关键字的复合语句或赋值语句。

当我们需要动态地计算表达式,而使用其它 Python 技术或工具会大大增加我们的开发时间和精力时,eval() 可以很方便。

在这一节中,我们将学习如何使用 Python 的 eval() 来计算布尔、数学和通用的 Python 表达式。

布尔表达式

布尔表达式 是Python表达式,当解释器对其进行计算时返回一个真值(True​ 或者 False​)。它们通常用在if语句中,以检查某些条件是否为真或假。由于布尔表达式不是复合语句,我们可以使用eval()来计算它们。

>>> x = 100
>>> y = 100
>>> eval("x != y")
False
>>> eval("x < 200 and y > 100")
False
>>> eval("x is y")
True
>>> eval("x in {50, 100, 150, 200}")
True
Copy after login

我们可以用 eval() 来处理使用以下任何Python运算符的布尔表达式。

  • 值比较运算符:< , > , <=, >=, ==, !=
  • 逻辑(布尔)运算符:and​,or​,not
  • 成员测试运算符:in​,not in
  • 身份运算符:is​,is not

在所有情况下,该函数都会返回正在计算的表达式的真值。

我们思考,为什么我应该使用eval()而不是直接使用布尔表达式呢?假设需要实现一个条件语句,但我们想临时改变条件。

>>> def func(a, b, condition):
... if eval(condition):
... return a + b
... return a - b
...
>>> func(2, 4, "a > b")
-2
>>> func(2, 4, "a < b")
6
>>> func(2, 2, "a is b")
4
Copy after login

在func()​中,使用eval()​来计算所提供的条件,并根据计算的结果返回a+b​或a-b​。在上面的例子中,只使用了几个不同的条件,但还可以使用任何数量的其他条件,只要坚持使用我们在func()​中定义的名称a​和b。

现在想象一下,如果不使用Python的eval(),我们将如何实现这样的东西。那会花更少的代码和时间吗?不可能!

数学表达式

Python 的 eval()​ 的一个常见用例是对基于字符串的输入进行 math​ 表达式的计算。例如,创建一个 Python 计算器,那么可以使用 eval() 来计算用户的输入并返回计算结果。

下面的例子演示了如何使用eval()​与数学一起进行math运算。

>>> # Arithmetic operations
>>> eval("5 + 7")
12
>>> eval("5 * 7")
35
>>> eval("5 ** 7")
78125
>>> eval("(5 + 7) / 2")
6.0
>>> import math
>>> # 一个圆的面积
>>> eval("math.pi * pow(25, 2)")
1963.4954084936207
>>> # 球体的体积
>>> eval("4 / 3 * math.pi * math.pow(25, 3)")
65449.84694978735
>>> # 直角三角形的斜边
>>> eval("math.sqrt(math.pow(10, 2) + math.pow(15, 2))")
18.027756377319946
Copy after login

当我们使用eval()​来计算数学表达式时,我们可以传入任何种类或复杂程度的表达式,eval()会解析它们,计算它们,如果一切正常,就会给我们预期结果。

通用表达式

前面我们已经学会了如何在布尔和 math​ 表达式中使用 eval()​ 。然而,我们可以在更复杂的 Python 表达式中使用 eval() ,这些表达式包括函数调用、对象创建、属性访问、列表推导式等等。

例如,可以调用一个内置函数或用标准或第三方模块导入的函数。

>>> # 运行echo命令
>>> import subprocess
>>> eval("subprocess.getoutput('echo Hello, World')")
'Hello, World'
>>> # 启动Firefox(如果有的话)
>>> eval("subprocess.getoutput('firefox')")
''
Copy after login

在这个例子中,我们使用 Python 的 eval()​ 来执行一些系统命令。我们可以用这个功能做大量有用的事情。然而,eval()也会有一些严重的安全风险,比如允许一个恶意的用户在我们的机器中运行系统命令或任何任意的代码。

The above is the detailed content of Python eval function dynamically evaluates mathematical expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!