Python的逗号运算符在赋值期间如何工作?

时间:2021-01-21 22:29:38

I was reading the assignment statements in the Python docs ( http://docs.python.org/reference/simple_stmts.html#assignment-statements ).

我正在阅读Python文档中的赋值语句(http://docs.python.org/reference/simple_stmts.html#assignment-statements)。

In that it is quoted that:

因此引用:

If the target is a target list enclosed in parentheses or in square brackets: The object must be an iterable with the same number of items as there are targets in the target list, and its items are assigned, from left to right, to the corresponding targets.

如果目标是括在括号中或方括号中的目标列表:对象必须是与目标列表中的目标具有相同数量的项目的可迭代对象,并且其项目从左到右分配给相应的目标。

After reading it, I thought of writing a sample like this:

读完之后,我想写一个这样的样本:

a = 5
b = 4
a, b = a + b, a
print a, b

My assumption was that a and b both should have the value of 9.

我的假设是a和b都应该具有9的值。

However, I am proven wrong. 'a' has the value of 9 and 'b' has the value of 5.

但是,我被证明是错的。 'a'的值为9,'b'的值为5。

Can some one help me understand this better? Why the older value of 'a' is assigned rather than the new one? As per the docs, a's value will be assigned first right? Am I missing something?

有人可以帮助我更好地理解这一点吗?为什么要分配旧值'a'而不是新值?根据文档,首先会分配一个值?我错过了什么吗?

3 个解决方案

#1


27  

All the expressions to the right of the assignment operator are evaluated before any of the assignments are made.

在进行任何赋值之前,将评估赋值运算符右侧的所有表达式。

From the Python tutorial: First steps towards programming:

从Python教程:编程的第一步:

The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

第一行包含多个赋值:变量a和b同时获取新值0和1.在最后一行再次使用它,证明右侧的表达式在任何赋值之前都是首先计算的发生。右侧表达式从左到右进行评估。

Emphasis mine.

Your code is functionally equivalent to the following:

您的代码在功能上等同于以下内容:

a, b = 5 + 4, 5
print a, b

#2


18  

Python does not have a "comma operator" as in C. Instead, the comma indicates that a tuple should be constructed. The right-hand side of

Python没有像C中那样的“逗号运算符”。相反,逗号表示应该构造元组。右边的

a, b = a + b, a

is a tuple with th two items a + b and a.

是一个元组,有两个项目a + b和a。

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.

在赋值的左侧,逗号表示应根据您引用的规则执行序列解包:a将分配元组的第一个元素,b为第二个元素。

#3


12  

You can think of the assignments happening in parallel on copies rather than sequentially and in-place.

您可以考虑在副本上并行发生的分配,而不是按顺序和就地进行。

This is why in python you dont need a swap function:

这就是为什么在python中你不需要交换功能:

a, b = b, a

works sufficiently without requiring a temp variable, c.

在不需要临时变量的情况下工作充分,c。

#1


27  

All the expressions to the right of the assignment operator are evaluated before any of the assignments are made.

在进行任何赋值之前,将评估赋值运算符右侧的所有表达式。

From the Python tutorial: First steps towards programming:

从Python教程:编程的第一步:

The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

第一行包含多个赋值:变量a和b同时获取新值0和1.在最后一行再次使用它,证明右侧的表达式在任何赋值之前都是首先计算的发生。右侧表达式从左到右进行评估。

Emphasis mine.

Your code is functionally equivalent to the following:

您的代码在功能上等同于以下内容:

a, b = 5 + 4, 5
print a, b

#2


18  

Python does not have a "comma operator" as in C. Instead, the comma indicates that a tuple should be constructed. The right-hand side of

Python没有像C中那样的“逗号运算符”。相反,逗号表示应该构造元组。右边的

a, b = a + b, a

is a tuple with th two items a + b and a.

是一个元组,有两个项目a + b和a。

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.

在赋值的左侧,逗号表示应根据您引用的规则执行序列解包:a将分配元组的第一个元素,b为第二个元素。

#3


12  

You can think of the assignments happening in parallel on copies rather than sequentially and in-place.

您可以考虑在副本上并行发生的分配,而不是按顺序和就地进行。

This is why in python you dont need a swap function:

这就是为什么在python中你不需要交换功能:

a, b = b, a

works sufficiently without requiring a temp variable, c.

在不需要临时变量的情况下工作充分,c。