Python 蟒蛇 a, b = b, a +b

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21990883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:02:59  来源:igfitidea点击:

Python a, b = b, a +b

python

提问by Ante

This is my first question and I started to learn Python. Is there a difference between:

这是我的第一个问题,我开始学习 Python。是否有区别:

a, b = b, a + b

and

a = b
b = a + b

When you write it in below example it shows different results.

当您在下面的示例中编写它时,它会显示不同的结果。

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
    print()
fib(1000)

and

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a = b
        b = a + b
    print()
fib(1000)

采纳答案by isedev

In a, b = b, a + b, the expressions on the right hand side are evaluated before being assigned to the left hand side. So it is equivalent to:

在 中a, b = b, a + b,右侧的表达式在分配给左侧之前先求值。所以它等价于:

c = a + b
a = b
b = c

In the second example, the value of ahas already been changed by the time b = a + bis run. Hence, the result is different.

在第二个例子中, 的值在运行a时已经改变了b = a + b。因此,结果是不同的。

回答by Martijn Pieters

The line:

线路:

a, b = b, a + b

is closer to:

更接近于:

temp_a = a
a = b
b = temp_a + b

where bis using the oldvalue of abefore awas reassigned to the value of b.

在哪里b使用之前的值被重新分配给 的值。aab

Python first evaluates the right-hand expression and stores the results on the stack, then takes those two values and assigns them to aand b. That means that a + bis calculated beforeais changed.

Python 首先计算右边的表达式并将结果存储在堆栈中,然后获取这两个值并将它们分配给ab。这意味着更改之前a + b计算。a

See How does swapping of members in the python tuples (a,b)=(b,a) work internally?for the low-down on how this all works, at the bytecode level.

请参阅python 元组 (a,b)=(b,a) 中成员的交换如何在内部工作?在字节码级别详细了解这一切是如何工作的。

回答by Wooble

Let's say we start with aand blike this:

假设我们开始ab喜欢这样:

a = 2
b = 3

So, when you do:

所以,当你这样做时:

a, b = b, a + b

what happens is you create the tuple (b, a + b)or (3, 5)and then unpack it into aand bso abecomes 3and bbecomes 5.

发生的事情是您创建元组(b, a + b)or(3, 5)然后将其解压缩到aand bso abecome3b变成5.

In your second example:

在你的第二个例子中:

a = b
# a is now 3
b = a + b
# b is 3 + 3, or 6.

回答by Lw Cui

I hope that you haven't been influenced by C language, which the priority of assignment operator =is higher than that of Comma operator ,. Do not think it's (a), (b = b), (a + b). It's a tuple assignment, meaning it's (a, b) = (b, a + b).

我希望你没有受到 C 语言的影响,它的赋值运算符的优先级=高于逗号运算符的优先级,。不要以为是(a), (b = b), (a + b)。这是一个元组分配,这意味着它是(a, b) = (b, a + b).

回答by SUMIT KHAJANCHI

a, b = b, a + bis similar to a, b = 0, 1assigning values to both variables a, bat same time. First assign a = band then b = a + b.

a, b = b, a + b类似于同时a, b = 0, 1为两个变量赋值a, b。先赋值a = b,然后b = a + b

回答by Calculus

Let's grok it.

让我们来了解一下。

a, b = b, a + b

a, b = b, a + b

It's a tuple assignment, means (a, b) = (b, a + b), just like (a, b) = (b, a)

这是一个元组赋值,意思是(a, b) = (b, a + b),就像(a, b) = (b, a)

Start from a quick example:

从一个简单的例子开始:

a, b = 0, 1
#equivalent to
(a, b) = (0, 1)
#implement as
a = 0
b = 1

When comes to (a, b) = (b, a + b)
EAFP, have a try directly

说到(a, b) = (b, a + b)
EAFP,直接试一试

a, b = 0, 1
a = b #a=b=1
b = a + b #b=1+1
#output
In [87]: a
Out[87]: 1
In [88]: b
Out[88]: 2

However,

然而,

In [93]: a, b = b, a+b
In [94]: a
Out[94]: 3
In [95]: b
Out[95]: 5

The result is different from the first try.

结果与第一次尝试不同。

Tha's because Python firstly evaluates the right-hand a+b
So it equivalent to:

Tha 是因为 Python 首先评估右手a+b
所以它相当于:

old_a = a
old_b = b
c = old_a + old_b
a = old_b
b = c

In summary, a, b = b, a+bmeans,
aexchanges to get old_value of b,
bexchanges to get the sum of old value aand old value b,

总之,a, b = b, a+b意思是,
a交换得到 old_value of b
b交换得到 old valuea和 old value的总和b

回答by Shaun T

There are differences between a,b = b,a+b and a=b b=a+b
let's have a look at the following two examples:

eg1:

a,b = b,a+b 和 a=bb=a+b 之间有区别,
让我们看一下以下两个示例:

eg1:

a,b = 0,1
while a<10:
  print(a)
  a,b = b,a+b

#output:
0
1
1
2
3
5
8

eg2:

例2:

a,b = 0,1
while a<10:
  print(a)
  a=b
  b=a+b
#output:
0
1
2
4
8

This is because the interpreter always calculates the figures in the right side of the Equals sign first. The calculation results will be assigned to the variables which on the left hand side only if all the calculation has been done on the right hand side.

这是因为解释器总是首先计算等号右侧的数字。只有在右侧完成所有计算后,才会将计算结果分配给左侧的变量。