gpt4 book ai didi

python - Python 中的未定义行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:21 24 4
gpt4 key购买 nike

CC++ 我知道有一种东西叫做

undefined behaviour

在表达式求值中,当某些表达式有副作用时。假设我要计算以下内容:

c = 10
f() + g() + c

但在某些时候 g 使 c = 5。(c 是一个 glob 变量)

python 中的行为是什么?它会被定义为 C 吗?

最佳答案

来自 的 6.15|文档

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4) <----- This is of importance to us.
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

所以这里的函数会按照从左到右的顺序调用。因此,您将看到的任何变化都是由从左到右调用的函数引起的。

中是的函数调用是表达式。

关于python - Python 中的未定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com