Python 3.x allows you to reset excess revenue for concurrent assignments using *
>>> a, b, *args = range(4) >>> a 0 >>> b 1 >>> args [2, 3]
But 2.x is not:
>>> a, b, *args = range(4) File "<stdin>", line 1 a,b,*args = range(4) ^ SyntaxError: invalid syntax
Is there any future import that might make this statement compatible with Python 2.x?
AFAIK this function is not available in Python 2, if you really need something like this - just write a utility function
def unpack(iterable, elements_count): iterator = iter(iterable) for _ in range(elements_count): yield next(iterator) # maybe use `list` or leave `iterator`-object yield tuple(iterator)
Then
a, b, args = unpack(range(4), 2)
will give the expected behavior
No, there is no equivalent support for unpacking Python 2.
In some cases, you can use slicing to approximate code for cross-compatibility:
first, second, rest = val[0], val[1], val[2:]
. , Python 2.7 __future__, .
__future__
(Note that the module __future__is not actually involved in the magic of future operations. This is mainly for documentation purposes and to avoid confusing tools that expect all imports to match the real module.)
Source: https://habr.com/ru/post/1674505/More articles:StackOverflowError in instance initializer - javaAndroid back button on progressive web application closes application - androidpython - password must contain at least one upper or lower case and number - pythonМногопроцессорность: порядок выполнения - pythonWhy python automatically returns the number of characters when writing a file - pythonEcto: Order pre-loaded data in collections using has_many association - elixirКаков путь Android SDK по умолчанию? - androidhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1674508/how-to-provide-environment-variables-to-aws-ecs-task-definition&usg=ALkJrhhjUM1NXCUWmUMuWVi547S35HZVKwhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1674509/allow-users-to-reload-php-fpm-without-sudo&usg=ALkJrhhsecm4loJeMQxVV14VAZqJ4QyKgwHow to define configuration in Swift code using Swift package manager - swiftAll Articles