항상 감사하며 마633

고정 헤더 영역

글 제목

메뉴 레이어

항상 감사하며 마633

메뉴 리스트

  • 홈
  • 태그
  • 방명록
  • 분류 전체보기 (93)
    • 개발 (59)
      • 개발환경설정 (6)
      • python-심화(Advanced) (23)
      • python-객체지향프로그래밍(OOP) (21)
      • python-병렬처리 (7)
      • python-속도향상(optimization) (2)
    • Study (16)
      • DeepLearning from scratch (16)
    • paper review (15)
      • NLP (15)
    • Google Machine Learning Boo.. (3)

검색 레이어

항상 감사하며 마633

검색 영역

컨텐츠 검색

type hint

  • Type Annotation (9) - Generic Type

    2022.05.25 by Matthew0633

  • Type Annotation (8) - Final Type

    2022.05.23 by Matthew0633

  • Type Annotation (7) - Type alias

    2022.05.23 by Matthew0633

  • Type Annotation (6) - Class

    2022.05.23 by Matthew0633

  • Type Annotation (5) - Optional

    2022.05.22 by Matthew0633

  • Type Annotation (4) - Union

    2022.05.22 by Matthew0633

  • Type Annotation (3) - Callable

    2022.05.22 by Matthew0633

  • Type Annotation (2) - 기본 자료구조 2 (List, Tuple, Dict, Set)

    2022.05.21 by Matthew0633

Type Annotation (9) - Generic Type

Generic Type A type that can be parameterized; typically a container class such as list or dict. Used for type hints and annotations. 파이썬 공식문서에서 는 generic type 을 위와 같이 설명하고 있다. 파이썬 내에서 Generic type class 는 type 을 파라미터화하여, 인자로 받을 수 있다. 인자로 받은 type 을 공유하는 지역변수 간에는 동일한 type 으로 표시될 수 있다. 이는 type hint와 annotation 을 위해 사용된다. 아래 예시를 보자. Robot class 를 정의할 때 T, K 라는 2개의 type 을 인자로 받는 Generic type 을 상속받는다..

개발/python-객체지향프로그래밍(OOP) 2022. 5. 25. 23:49

Type Annotation (8) - Final Type

Final Type 변수에 대해 한번 값이 정해지고, 이후 값이 변경되지 않는 상수임을 나타낼 때, Final 을 사용할 수 있다. from typing_extensions import Final BATCH_SIZE: Final = 128

개발/python-객체지향프로그래밍(OOP) 2022. 5. 23. 14:20

Type Annotation (7) - Type alias

변수의 사용가능한 type 이 많아 가독성이 떨어질 경우, alias 를 부여할 수 있다. # https://mypy.readthedocs.io/en/stable/kinds_of_types.html#type-aliases from typing import Union, List, Tuple, Dict, Optional # type alias Type_x = Union[int, Union[List[str], Tuple[int, ...]], Optional[Dict[str, float]]] x: Type_x = 17 def cal(v: Type_x ) -> Type_x : return v Dict alias : TypedDict dict 의 경우는 class 를 활용하여 type alias 를 정의할 수 있다..

개발/python-객체지향프로그래밍(OOP) 2022. 5. 23. 14:20

Type Annotation (6) - Class

변수의 class 를 hint 로 제공할 경우, 단순히 class명을 변수명 옆에 써주면 된다. # class types class Hello: def world(self) -> int: return 7 # (1) 변수 정의 시 class hint myhello: Hello = Hello() # (2) 함수 인자에서 class hint def foo(ins: Hello) -> int: return ins.world() print(foo(hello)) # 7

개발/python-객체지향프로그래밍(OOP) 2022. 5. 23. 14:20

Type Annotation (5) - Optional

Optional Type 변수의 값이 특정 type 이거나 혹은 None 일 때, Optional 을 사용하여 명시할 수 있다. Optional[type1]은 Union[type1, None]과 같게 여겨질 수 있다. # * Optional type from typing import Union, Optional def foo(name: str) -> Optional[str]: if name == "fancy": return None else: return name name1: Optional[str] = foo("nobody") # None name2: Optional[str] = foo("fancy") # fancy

개발/python-객체지향프로그래밍(OOP) 2022. 5. 22. 20:53

Type Annotation (4) - Union

Union Type 특정 변수의 자료형이 여러 type 중 하나이면 문제 없을 때, 가능한 type의 범위를 Union을 통해 나타낼 수 있다. # https://mypy.readthedocs.io/en/stable/kinds_of_types.html import numpy as np from typing import Union, List arr1: Union[List, np.array] = [1,2,3] def foo(arr: Union[List[int], np.array[int]) -> int: return arr[0] print(foo(arr1)) # 1

개발/python-객체지향프로그래밍(OOP) 2022. 5. 22. 20:52

Type Annotation (3) - Callable

Callable Type Callable 객체에 대해 type hint를 작성할 때는, 파라미터들의 type들을 list 형태로 묶고, return 값의 type 과 함께 더 큰 list 형태로 묶어서 표시한다. 예를 들어, 아래 예시에서, foo 의 func 은 callable 객체를 나타내며, 해당 객체는 int 형의 두 개의 인자를 받고, int 형을 반환한다. # https://mypy.readthedocs.io/en/stable/kinds_of_types.html # * Callable types from typing import Callable def add(a: int, b: int) -> int: return a + b def foo(func: Callable[[int, int], int]..

개발/python-객체지향프로그래밍(OOP) 2022. 5. 22. 20:52

Type Annotation (2) - 기본 자료구조 2 (List, Tuple, Dict, Set)

기본 자료구조 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 의 자..

개발/python-객체지향프로그래밍(OOP) 2022. 5. 21. 23:43

추가 정보

인기글

최신글

페이징

이전
1 2
다음
TISTORY
항상 감사하며 마633 © Magazine Lab
페이스북 트위터 인스타그램 유투브 메일

티스토리툴바