Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
nibor48775yI can probably count on one hand the number of times I have wanted to swop two variables in a real life application.
-
nekokatt885yyeah, 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 -
nekokatt885y@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; -
Parzi88335yTHIS IS A 1:1 REPOST OF ANOTHER POST FROM HERE.
well the pic is replaced with text but still
Related Rants
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
joke/meme
variable
python