learn-python

Форк
0
/
test_function_annotations.py 
27 строк · 1.0 Кб
1
"""Function Annotations.
2

3
@see: https://docs.python.org/3/tutorial/controlflow.html#function-annotations
4

5
Function annotations are completely optional metadata information about the types used
6
by user-defined functions.
7

8
Annotations are stored in the __annotations__ attribute of the function as a dictionary and have no
9
effect on any other part of the function. Parameter annotations are defined by a colon after the
10
parameter name, followed by an expression evaluating to the value of the annotation. Return
11
annotations are defined by a literal ->, followed by an expression, between the parameter list and
12
the colon denoting the end of the def statement.
13
"""
14

15

16
def breakfast(ham: str, eggs: str = 'eggs') -> str:
17
    """Breakfast creator.
18

19
    This function has a positional argument, a keyword argument, and the return value annotated.
20
    """
21
    return ham + ' and ' + eggs
22

23

24
def test_function_annotations():
25
    """Function Annotations."""
26

27
    assert breakfast.__annotations__ == {'eggs': str, 'ham': str, 'return': str}
28

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.