如何证明参数评估是“从左到右"?在Python中? [英] How to prove that parameter evaluation is "left to right" in Python?

查看:118
本文介绍了如何证明参数评估是“从左到右"?在Python中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在JavaScript中,我们可以编写如下程序:

For example, in JavaScript we could write a program like this:

var a = 1;
testFunction(++a, ++a, a);
function testFunction(x, y, z){
      document.writeln("<br />x = " + x);
      document.writeln("<br />y = " + y);
      document.writeln("<br />z = " + z);
}

我们将得到一个输出:

x = 2
y = 3
z = 3

这意味着在JavaScript中参数是从左到右真正评估的.在C语言中,我们将获得输出

This implies that parameters are truly evaluated from left to right in JavaScript. In C we would get output

x = 3
y = 3
z = 3

我想知道我们是否可以在Python中做同样的事情,或者因为它是一种按值传递参考语言,所以这是不可能的吗?

I was wondering if we could do the same in Python or is it impossible since it's a pass by value reference language?

我编写了一个简单的程序,但我认为这不能证明任何事情:

I've made a simple program but I don't think that proves anything:

x = 2
def f(x, y, z):
    print(x, y, z)

f(x*2, x*2, x**2)
print(x)

4 4 4
2

在调用它时,Python不允许我在函数参数内进行任何新赋值(例如f(x=4, x, x)或类似的东西).

Python won't let me do any new assignment within the function parameter when I call it (for example f(x=4, x, x) or something like this).

推荐答案

>>> def f(x, y): pass
...
>>> f(print(1), print(2))
1
2

这篇关于如何证明参数评估是“从左到右"?在Python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆