13

Swap variable without temporary variable
meanwhile C:
a=a+b
b=a-b
a=a-b

while python:
a,b=b,a

Python be like:B*tch Please

Comments
  • 6
    Parallel assignment is like the single good thing about Python
  • 13
    I can probably count on one hand the number of times I have wanted to swop two variables in a real life application.
  • 1
    @FrodoSwaggins Well, yeah I just mean the syntax. Fuck Python
  • 1
    Python do it because is high level language ... End
  • 0
    @jak645 They do it because it's cool. That's the only cool thing
  • 1
    yeah, when you do that in python, it is actually doing this under the hood, essentially:

    # a, b = 1, 2
    _ = (1, 2)
    a = _[0]
    b = _[1]

    you just dont usually care because Python is slowish anyway in comparison to some other languages, but you could find a performance hit by doing this in certain situations. It is implementation specific though
  • 0
    @nekokatt additionally, on ARM and x86, you tend to use xor rather than +- to swap:

    int a = 132;
    int b = 67;

    a ^= b ^= a ^= b;
  • 0
    C:
    a ^= b;
    b ^= a;
    a ^= b;

    ^ is bitwise XOR

    Don't ask me why it works.
  • 0
    THIS IS A 1:1 REPOST OF ANOTHER POST FROM HERE.

    well the pic is replaced with text but still
Add Comment