Is there any difference in Python between unpacking into a tuple:
x, y, z = v
and unpacking to a list?
[x, y, z] = v
Absolutely nothing, right down to the bytecode (using dis):
dis
>>> def list_assign(args): [x, y, z] = args return x, y, z >>> def tuple_assign(args): x, y, z = args return x, y, z >>> import dis >>> dis.dis(list_assign) 2 0 LOAD_FAST 0 (args) 3 UNPACK_SEQUENCE 3 6 STORE_FAST 1 (x) 9 STORE_FAST 2 (y) 12 STORE_FAST 3 (z) 3 15 LOAD_FAST 1 (x) 18 LOAD_FAST 2 (y) 21 LOAD_FAST 3 (z) 24 BUILD_TUPLE 3 27 RETURN_VALUE >>> dis.dis(tuple_assign) 2 0 LOAD_FAST 0 (args) 3 UNPACK_SEQUENCE 3 6 STORE_FAST 1 (x) 9 STORE_FAST 2 (y) 12 STORE_FAST 3 (z) 3 15 LOAD_FAST 1 (x) 18 LOAD_FAST 2 (y) 21 LOAD_FAST 3 (z) 24 BUILD_TUPLE 3 27 RETURN_VALUE
No. In fact, it x, y, z = vis an abbreviation for:
(x, y, z) = v
... which is unpacked into a tuple. The same behavior occurs:
>>> v = (1, 3, 4) >>> [x, y, z] = v >>> x, y, z (1, 3, 4) >>> x, y, z = v >>> x, y, z (1, 3, 4)
Source: https://habr.com/ru/post/1541780/More articles:Как запустить экземпляр EC2 в VPC с общедоступным IP-адресом в PowerShell? - powershellHow does the Conditional attribute work? - c #how to clear a SublimeREPL window in Sublime 2 - sublimetext2Воспроизвести видео YouTube в полноэкранном режиме и разрешить поворот только для видео для iOS7 и iOS8 - youtubeScrolling does not work in Internet Explorer - javaWhy Ruby cross_product returns a different value from a cross product on paper - ruby | fooobar.comAudioSessionSetProperty в Xamarin/Monotouch - iosgrep (awk) file from A to the first empty line - bashFull Width Image - Foundation - htmlЯвляется ли создание нового объекта потоком безопасным - javaAll Articles