4 ms·
Because few languages have the ability to create a tuple with syntactic sugar on the fly that Python has. In PHP, you can similarly do the following: > list($a
by Sebb767 5y ago
Because few languages have the ability to create a tuple with syntactic sugar on the fly that Python has. In PHP, you can similarly do the following:
> list($a, $b) = [$b, $a];
Which is a bit more verbose than what Python does, but it's the same thing under the hood - you create a list/tuple, create a new list with swapped items at specific indices and then dissolve that list back to local variables.
In the end, this is more expensive than the swap you'd do in C++^W other languages (EDIT: apparently C++ can also do this now), since it involves tuple creation. Although the compiler/JIT/interpreter will most likely recognize this pattern and optimize it to a `swap` call.
- adastra22 5y agoC++ can do multiple assignment now, fyi.
- tored 5y agoSince PHP 7.1 you can write [$a, $b] = [$b, $a];
- Koiwai 5y agoLots of newer languages can, like go and rust. BTW it's not "creating a tuple with syntactic sugar on the fly", I mean, if you think that's creating a tuple, obviously in lots of languages, that tuple is created by passing x/y's value, assigning to that anonymous tuple won't affect original x/y. The correct term should be destructuring assignment.