42

Thats how you do it๐Ÿ˜Ž

Comments
  • 9
    Python probably uses a dozen variables to actually do it. The challenge is obviously to reduce memory requirements of the is operation.
  • 1
    @Wozza365 No, it probably adds 2 temporary variables
  • 9
    a ^= b
    b ^= a
    a ^= b
  • 2
    @Mitiko yes thats the issue
  • 0
    @Wozza365 @Mitiko @Batburger
    No, it does not use any extra variables. It just creates a tuple of right hand side and compares and assigns to left hand side.
    More details->
    https://stackoverflow.com/questions...
  • 0
    @HakunaMatata Well it still greates a Tuple which requires to be allocated in memory.
  • 0
    @localghost Let's hope that a!=b...
  • 1
    @tomiolah1998

    Yes or a will be 0
  • 0
    @Wozza365 by thinking of it that way ... I am really curious about how python is handling this behind the scenes ...
    Without punishing in some language-war-discussions.

    I am using python myself often for some things, but all are totally unrelated to realtime, memory performance and I just got interested about how these things are handled in the back ...

    Because,
    Placing the value of b in a, makes it unable to assign the value of a in b, since a is already b. So a is basically replaced by b and we lost the old value of a
  • 2
    And in ES6:
    [a, b] = [b, a];
  • 1
    Why swap variables tho?
Add Comment