Python a, b = b, a +b

时间:2022-01-07 00:16:03

This is my first question and i started to learn Python so i will just ask. Is there 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)

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

6 个解决方案

#1


25  

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 a has already been changed by the time b = a + b is run. Hence, the result is different.

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

#2


13  

The line:

线:

a, b = b, a + b

is closer to:

更接近:

temp_a = a
a = b
b = temp_a + b

where b is using the old value of a before a was reassigned to the value of b.

这里b用的是a的旧值然后a被重新赋值给b。

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

Python首先对右边的表达式求值,并将结果存储在堆栈上,然后将这两个值赋给a和b,这意味着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)中的成员交换在内部如何工作?在字节码级别上,对于这一切如何工作的内幕。

#3


5  

Let's say we start with a and b like this:

假设我们从a和b开始,像这样:

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 a and b so a becomes 3 and b becomes 5.

发生的是你创建了元组(b, a + b)或者(3,5)然后解压缩到a和b中,a变成3,b变成5。

In your second example:

在第二个例子中:

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

#4


0  

a, b = b, a + b is similar to a, b = 0, 1 assigning values to both variables a, b at same time. First assign a = b and then b = a + b.

a, b = b, a + b与a, b = 0, 1同时赋值给两个变量a, b。首先分配a = b,然后b = a + b。

#5


0  

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)

#6


0  

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:

这是因为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+b means,
a exchanges to get old_value of b,
b exchanges to get the sum of old value a and old value b,

综上所述,a, b = b, a+b表示a交换b的old_value, b交换b的old value a和old value b的和,

#1


25  

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 a has already been changed by the time b = a + b is run. Hence, the result is different.

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

#2


13  

The line:

线:

a, b = b, a + b

is closer to:

更接近:

temp_a = a
a = b
b = temp_a + b

where b is using the old value of a before a was reassigned to the value of b.

这里b用的是a的旧值然后a被重新赋值给b。

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

Python首先对右边的表达式求值,并将结果存储在堆栈上,然后将这两个值赋给a和b,这意味着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)中的成员交换在内部如何工作?在字节码级别上,对于这一切如何工作的内幕。

#3


5  

Let's say we start with a and b like this:

假设我们从a和b开始,像这样:

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 a and b so a becomes 3 and b becomes 5.

发生的是你创建了元组(b, a + b)或者(3,5)然后解压缩到a和b中,a变成3,b变成5。

In your second example:

在第二个例子中:

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

#4


0  

a, b = b, a + b is similar to a, b = 0, 1 assigning values to both variables a, b at same time. First assign a = b and then b = a + b.

a, b = b, a + b与a, b = 0, 1同时赋值给两个变量a, b。首先分配a = b,然后b = a + b。

#5


0  

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)

#6


0  

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:

这是因为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+b means,
a exchanges to get old_value of b,
b exchanges to get the sum of old value a and old value b,

综上所述,a, b = b, a+b表示a交换b的old_value, b交换b的old value a和old value b的和,