4.7.7. Function Annotations
함수에 주석달기
Function annotations are completely optional metadata information about the types used by user-defined functions (see PEP 484 for more information).
함수에 주석을 다는 것은 사용자 정의 함수 등의 유형을 위한 선택적 사항입니다.
Annotations are stored in the __annotations__
attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation.
Return annotations are defined by a literal ->
, followed by an expression, between the parameter list and the colon denoting the end of the def
statement. The following example has a positional argument, a keyword argument, and the return value annotated:
주석은 함수의__annotation__
속성 안에 사전의 유형으로 저장되며, 함수의 다른 부분에 아무 영향도 미치지 않습니다. 매개변수에 대한 주석은 콜론에 의해 매개변수의 이름 뒤에 쓰여지며, 그 이름 뒤에는 주석의 값을 측정하는 표현식이 따라옵니다.
retrun annotation은 표현식 뒤에 따라오는 ->
문자에 의해 이뤄집니다. 위치는 매개변수의 목록과 함수의 끝을 나타내는 콜론(:) 사이 입니다. 다음 예시는 하나의 위치 인수와 하나의 키워드 인수를 가지고 있습니다. 그리고 반환되는 값에 주석이 붙습니다.
>>> def f(ham: str, eggs: str = 'eggs') -> str:
... print("Annotations:", f.__annotations__)
... print("Arguments:", ham, eggs)
... return ham + ' and ' + eggs
...
>>> f('spam')
Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}
Arguments: spam eggs
'spam and eggs'