增强赋值运算符python,简单的赋值运算符在Python中变得复杂

I have declared four variables [a=1,b=2,c=3,d=0] in python and swapping them in one line code using ',' and '=' (Simple Assignment Operator).

I have got multiple Answers and got confused. please help me...

Case 1:

a=1

b=2

c=3

d=0

a=a,b=b,c

print "a = " + str(a)

print "b = " + str(b)

print "c = " + str(c)

print "d = " + str(d)

Output of Case 1:

a = 2

b = 3

c = 3

d = 0

Case 2:

a=1

b=2

c=3

d=0

b=a,b=b,c

print "a = " + str(a)

print "b = " + str(b)

print "c = " + str(c)

print "d = " + str(d)

Output of Case 2:

a = 2

b = 3

c = 3

d = 0

Case 3:

a=1

b=2

c=3

d=0

c=a,b=b,c

print "a = " + str(a)

print "b = " + str(b)

print "c = " + str(c)

print "d = " + str(d)

Output of Case 3:

a = 2

b = 3

c = (2,3)

d = 0

Case 4:

a=1

b=2

c=3

d=0

d=a,b=b,c

print "a = " + str(a)

print "b = " + str(b)

print "c = " + str(c)

print "d = " + str(d)

Output of Case 4:

a = 2

b = 3

c = 3

d = (2,3)

Confusion is:

In the case number 3 and 4 the output is correct (as i expected). but in the case number 1 and 2 the value of a is 2 and value of b is 3. I expect the value should be (2,3). So what's problem in my code?

[My Python Version is 2.7]

解决方案

tl;dr: multiple assignments (multiple = statements on one line) are evaluated from left-to-right, not from right-to-left (after evaluating the right-hand-side expression).

To complicate matters, you are using tuple assignment and 'normal' assignment in a heady mix.

Tuple assignment uses one assignment operator, so to swap two variables, use:

a, b = b, a

The right-hand side must evaluate to a tuple of the same number of elements as there are variables on the left-hand side. You do that, so that's fine.

Now, in your examples, you are not only unpacking tuples. When the left-hand side contains only one variable, the tuple is not unpacked, just simply assigned:

a, b = 1, 2

a = b, a

becomes (2, 1).

The fun starts when you use multiple assignments on the same line. These are processed from left to right.

So, the following simple example:

a = b = c = 1

means that a becomes 1, then b, then c.

Now we can understand each case:

a=a,b=b,c, where a = 1, b = 2, c = 3.

This becomes: evaluate b, c -> (2, 3), then assign that to a -> a = (2, 3). Then assign it to a, b, so a = 2, b = 3. Result: a = 2, b = 3, c = 3.

b=a,b=b,c, where a = 1, b = 2, c = 3.

Same as the case before, but now b = (2, 3) is set first, then b = 3 again, same result as case 1.

c=a,b=b,c, where a = 1, b = 2, c = 3.

Input on the right-hand side the same as cases 1. and 2., but now we first set c = (2, 3). End result as expected, a = 2, b = 3, c = (2, 3).

d=a,b=b,c, where a = 1, b = 2, c = 3.

Same as case 3. but now we set d instead. No surprises.

What confused you here is that the assignments, after the right-hand side has been evaluated, are processed from left to right, not from right to left.

For cases like these, it's actually easiest to run your code (wrapped in a function), through the dis.dis() function to disassemble the python bytecode:

>>> import dis

>>> def f(): a=a,b=b,c

...

>>> dis.dis(f)

1 0 LOAD_FAST 0 (b)

3 LOAD_GLOBAL 0 (c)

6 BUILD_TUPLE 2

9 DUP_TOP

10 STORE_FAST 1 (a)

13 UNPACK_SEQUENCE 2

16 STORE_FAST 1 (a)

19 STORE_FAST 0 (b)

22 LOAD_CONST 0 (None)

25 RETURN_VALUE

This is the first case; note how after the BUILD_TUPLE and the DUP_TOP opcode (the latter creates an extra copy on the stack to serve the extra assignment), the first thing that happens is the STORE_FAST operation on a, followed by a UNPACK_SEQUENCE (the tuple assignment opcode), to then store the results into a and b.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值