[Python] Annotation
Contents
Annotation이란?
- annotation은 function의 parameter와 return value에 대한 hint를 주는 것이다.
- 강제하는 것이 아니라 단지 알려주는 것이다.
- 파이썬은 동적으로 type을 결정하기 때문에 잘못된 type을 사용할 수 있다.
문법
Parameters
- parameter 뒤에 annotation을 작성한다.
- type을 지정할 수도 있고, defaule value를 지정할 수도 있다.
|
|
- *args와 **kwargs에도 적용된다.
|
|
- 중첩해서 사용할 수도 있다.
|
|
Return Values
- “->” 뒤에 expression을 작성한다.
|
|
Lambda
- lambda 문법에는 annotation이 적용되지 않는다.
Annotation attribute
- annotation은 __annotations__ attribute로 접근이 가능하다.
참조 : PEP-3107