Annotation이란?

  • annotation은 function의 parameter와 return value에 대한 hint를 주는 것이다.
  • 강제하는 것이 아니라 단지 알려주는 것이다.
  • 파이썬은 동적으로 type을 결정하기 때문에 잘못된 type을 사용할 수 있다.

문법

Parameters

  • parameter 뒤에 annotation을 작성한다.
  • type을 지정할 수도 있고, defaule value를 지정할 수도 있다.
1
2
3
4
def foo(a: expression, b: expression=5):
    ...
def foo(a: str, b: float=1.5):
    ...
  • *args와 **kwargs에도 적용된다.
1
2
def foo(*args: expression, **kwargs: expression):
    ...
  • 중첩해서 사용할 수도 있다.
1
2
3
def foo((x1, y1: expression),
        (x2: expression, y2: expression)=(None, None)):
    ...

Return Values

  • “->” 뒤에 expression을 작성한다.
1
2
3
4
def sum() -> expression:
    ...
def sum() -> int:
    ...

Lambda

  • lambda 문법에는 annotation이 적용되지 않는다.

Annotation attribute

  • annotation은 __annotations__ attribute로 접근이 가능하다.
    annotation_attribute

참조 : PEP-3107