A Python Shorthand for Swapping Two Variables

preview_player
Показать описание
Because Python is amazing it has an elegant shorthand syntax for swapping two variables without having to use a third temporary variable: a,b = b, a

Рекомендации по теме
Комментарии
Автор

just learning python and I'm glad you mentioned that it was basically making a tuple from the values because that's what I was thinking to do before watching the vid :)

LollipopWoodworksNWA
Автор

Just keep in mind that, despite it looking like a single operation, this operation is not atomic in Python. You still need a lock if you are swapping variables that are being accessed by multiple threads.

rrrrrrrrrrrrrrrrrrrrr
Автор

the a, b = b, a is beeter than any way i have seen all of youtube

sowaaa
Автор

Python is cool ... but here, is this cool, or is it superior?
In terms of the code, it looks simpler, cleaner, that is for sure.
But, behind the scenes, let say you have to do it a million times ... is it slower, the same, faster.
I have no idea what goes on behind the scenes with Python, and I would like to find out, but
I'm thinking here, this doesn't make much difference.

justgivemethetruth
visit shbcf.ru