Thread: Another doubt.

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Another doubt.


    Heres my code:
    >>> X = 'spam'
    >>> Y = 'eggs'
    >>> X, Y = Y, X
    >>> X
    'eggs'
    >>> Y
    'spam'
    When I run X, Y = Y and X individually, the values arent changes but when I run them in a single statement, the values are swapped. Why so?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Originally Posted by Akshat1
    When I run X, Y = Y and X individually, the values arent changes
    I don't understand what do you mean, write down here exactly what you write in your Python session which does not work.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by Akshat1
    When I run X, Y = Y and X individually, the values arent changes but when I run them in a single statement, the values are swapped. Why so?
    Because its been defined that way. Of course when you type:

    Code:
    >>> x
    no values are changed. Why should they? The values of the variables are changed with =, and

    Code:
    >>> x, y = y, x
    has been defined so that it can swap the values, a very handy operation.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Commas are not a statement separator in Python. In this case, they're being used to separate items in sequences. It may be clearer if you add parentheses:
    Code:
    >>> x = "spam"
    >>> y = "eggs"
    >>> (x, y) = (y, x)
    >>> x
    'eggs'
    >>> y
    'spam'
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    Originally Posted by Akshat1
    Heres my code:
    >>> X = 'spam'
    >>> Y = 'eggs'
    >>> X, Y = Y, X
    >>> X
    'eggs'
    >>> Y
    'spam'
    When I run X, Y = Y and X individually, the values arent changes but when I run them in a single statement, the values are swapped. Why so?

    Because the swap occurs simultaneously.

    You can view it like this... If you type "x=y", then x literally is y. So when you type y=x after that, you're only saying y=y.

    When you do it in the single action "x,y=y,x" you load the values of y and x then swap them at the same time. So at the end, y does NOT == y.

    It's kind of confusing but it's actually nice to know how to swap variables like that. For the longest time I had to do something like:

    temp=x
    x=y
    y=temp


    Which is just annoying when you can do it all in one line of code

    Comments on this post

    • Dietrich agrees : sounds like the old C days
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Nyktos
    Commas are not a statement separator in Python. In this case, they're being used to separate items in sequences. It may be clearer if you add parentheses:
    Code:
    >>> x = "spam"
    >>> y = "eggs"
    >>> (x, y) = (y, x)
    >>> x
    'eggs'
    >>> y
    'spam'
    OK, I tried it your way cuz that seemed like the easiest one. But, I am confused even after adding parentheses:
    Code:
    >>> (x,y) = (y=x) # This means x = y (x referencing to ys object) and y = x (y referencing to xs value which was earlier assigned to y itself!)
    And when I run the code, both y and x display the same value. You said that x,y = y,x is equivalent to (x,y) = (y,x).
    But also, this is also equivalent to type:
    Code:
    x = y; y = x
    Here, the values are the same but not in (x,y) = (y,x). Why so?
    P.S. * arent changed (not changes, in the main post)
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    As Nyktos said,

    (a,b) = (b,a)

    First the right hand side evaluates.

    (b,a) is a tuple.

    Then the assignment occurs.

    It's a tuple assignment.

    Try this experiment:

    (a,b,c,d) = range(4)
    print(d)
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Semicolons are very different from commas. This...
    Code:
    x; y = y; x
    ...is equivalent to this...
    Code:
    x
    y = y
    x
    ...which is very different from this...
    Code:
    x, y = y, x
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    If you look at the byte-code produced you notice a rotate action:
    Code:
    import dis
    
    def myfunc():
        x = 2
        y = 7
        y , x = x , y
        return y , x
        
    
    byte_code = dis.dis(myfunc)
    
    print(byte_code)
    
    ''' result ...
      8           0 LOAD_CONST               1 (2)
                  3 STORE_FAST               0 (x)
    
      9           6 LOAD_CONST               2 (7)
                  9 STORE_FAST               1 (y)
    
     10          12 LOAD_FAST                0 (x)
                 15 LOAD_FAST                1 (y)
                 18 ROT_TWO             
                 19 STORE_FAST               1 (y)
                 22 STORE_FAST               0 (x)
    
     11          25 LOAD_FAST                1 (y)
                 28 LOAD_FAST                0 (x)
                 31 BUILD_TUPLE              2
                 34 RETURN_VALUE        
    None
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    OK, now I got it.
    Thanks everyone

IMN logo majestic logo threadwatch logo seochat tools logo