[Python] 어노테이션(annotation), typing
2023. 4. 10. 13:39
타입 어노테이션(type annotation)
타입에 대한 힌트 제공 (다른 type을 입력해도 에러가 발생하지 않음)
# 변수 선언
name:str = 'sso'
# 함수 매개변수, 반환값
def add(a: int, b: int) -> int:
return a + b
typing 모듈
타입 체크 (다른 type을 입력하면 에러 발생)
from typing import List, Dict, Tuple, Set, Union, Final
# List
def add(nums:List[int]) -> int:
return sum(n for n in nums)
# Dict (key type, value type)
dic: Dict[str, int] = {'sso', '1'}
※ python 3.9이상부터는 typing 모듈 import없이 사용 가능
dic: dict[str, int] = {'sso', '1'}
'Python > 파이썬 중급' 카테고리의 다른 글
[Python] 코루틴 사용법 (1) | 2024.01.26 |
---|---|
[Python] 코루틴 (Coroutine) (0) | 2023.02.16 |
[Python] 이터레이터(iterator), 제너레이터(generator) (0) | 2022.12.14 |
[Python] setdefault 함수 (0) | 2022.12.11 |
[Python] sorted, sort 함수 (0) | 2022.12.11 |