Multiple assignment and evaluation order in Python

前端 未结 10 2071
醉酒成梦
醉酒成梦 2020-11-22 01:41

What is the difference between the following Python expressions:

# First:

x,y = y,x+y

# Second:

x = y
y = x+y

First gives diffe

10条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 02:20

    The first one is a tuple-like assignment:

    x,y = y,x+y
    

    Where x is the first element of the tuple, and y is the second element, thus what you are doing is:

    x = y
    y = x+y
    

    Wheras the second is doing a straight assign:

    x=y
    x=x+y
    

提交回复
热议问题