개발/python-객체지향프로그래밍(OOP)
Type Annotation (2) - 기본 자료구조 2 (List, Tuple, Dict, Set)
Matthew0633
2022. 5. 21. 23:43
기본 자료구조 2 (List, Tuple, Dict, Set)
여러 원소를 담을 수 있는 기본 자료구조형들의 경우 typing module 을 불러와서 해당 자료형 이름의 class로 hint 를 제공할 수 있다. 작성방식은 다른 자료형과 동일하다.
from typing import List, Tuple, Dict, Set
list_var: List[str] = ["1", "2", "3"]
tuple_var: Tuple[int, ...] = (1, 3, 4)
dic_var: Dict[str, int] = {"hello": 47}
set1: Set[str] = {'a', 'b', 'c', 'd'}
def cal_add(numbers: List[int]) -> int:
# 함수 내에서 argument 의 자료형 검사 수행
assert isinstance(obj, List) f"obj should be List"
return sum(numbers)
print(cal_add([1, 3])) # 4
print(cal_add((1, 3)) # AssertionError