Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
Python functions can return multiple values: def edges(lst): return lst[0], lst[-1] first, last = edges([1, 2, 3]) assert first == 1 assert last == 3 In truth, lst[0], lst[-1] is a simple tuple. It's returned as usual and then unpacked to first and last: result = edges([1, 2, 3]) assert isinstance(result, tuple) first, last = result assert first == 1 assert last == 3 Usually, you don't care about it at all. However, all these things come to the surface when you use type hints. You have to define the function return value as tuple: def edges(lst) -> Tuple[int, int]: return lst[0], lst[-1] Calling that function is even harder. You may think that you can do something like this: first: int, last: int = edges([1, 2, 3]) Or at least this: first, last: Tuple[int, int] = edges([1, 2, 3]) But both ways are incorrect. This is the only reasonable thing you can do to annotate these variables: first: int last: int first, last = edges([1, 2, 3])