Multiple assignment and evaluation order in Python

前端 未结 10 1905
醉酒成梦
醉酒成梦 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:03

    It is explained in the docs in the section entitled "Evaluation order":

    ... while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

    0 讨论(0)
  • 2020-11-22 02:03

    I've recently started using Python and this "feature" baffled me. Although there are many answers given, I'll post my understanding anyway.

    If I want to swap the values of two variables, in JavaScipt, I'd do the following:

    var a = 0;
    var b = 1;
    
    var temp = a;
    a = b;
    b = temp;
    

    I'd need a third variable to temporarily hold one of the values. A very straightforward swap wouldn't work, because both of the variables would end up with the same value.

    var a = 0;
    var b = 1;
    
    a = b; // b = 1 => a = 1
    b = a; // a = 1 => b = 1
    

    Imagine having two different (red and blue) buckets and having two different liquids (water and oil) in them, respectively. Now, try to swap the buckets/liquids (water in blue, and oil in red bucket). You can't do it unless you have an extra bucket.

    Python deals with this with a "cleaner" way/solution: Tuple Assignment.

    a = 0
    b = 1
    
    print(a, b) # 0 1
    
    # temp = a
    # a = b
    # b = temp
    
    a, b = b, a # values are swapped
    
    print(a, b) # 1 0
    

    I guess, this way Python is creating the "temp" variables automatically and we don't have to worry about them.

    0 讨论(0)
  • 2020-11-22 02:11

    The first expression:

    1. Creates a temporary tuple with value y,x+y
    2. Assigned in to another temporary tuple
    3. Extract the tuple to variables x and y

    The second statement is actually two expressions, without the tuple usage.

    The surprise is, the first expression is actually:

    temp=x
    x=y
    y=temp+y
    

    You can learn more about the usage of comma in "Parenthesized forms".

    0 讨论(0)
  • 2020-11-22 02:11

    Other answers have already explained how it works, but I want to add a really concrete example.

    x = 1
    y = 2
    x, y = y, x+y
    

    In the last line, first the names are dereferenced like this:

    x, y = 2, 1+2
    

    Then the expression is evaluated:

    x, y = 2, 3
    

    Then the tuples are expanded and then the assignment happens, equivalent to:

    x = 2; y = 3
    
    0 讨论(0)
  • 2020-11-22 02:17

    In the second case, you assign x+y to x

    In the first case, the second result (x+y) is assigned to y

    This is why you obtain different results.

    After your edit

    This happen because, in the statement

    x,y = y,x+y
    

    all variables at the right member are evaluated and, then, are stored in the left members. So first proceed with right member, and second with the left member.

    In the second statement

    x = y
    y = x + y
    

    yo first evaluated y and assign it to x; in that way, the sum of x+y is equivalent to a sum of y+y and not of x+x wich is the first case.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题