Skip to content
Minsing Log
Go back

English Edition

What are : and ->?

Programming & CS

One-line explanation

Using annotations

  1. : is a comment describing the parameters.
  2. -> is a comment describing the return value.

By specifying the data types for both, you can avoid inputting incorrect data types or having to look up the data types of output values individually.

Example code explanation

Class attributes and regular attributes

from typing import ClassVar, Dict

class BasicStarship:
    captain: str = 'Picard'               # 기본값이 있는 일반 속성
    damage: int                           # 기본값이 없는 일반 속성
    stats: ClassVar[Dict[str, int]] = {}  # 클래스 속성

Functions

def executeCallback(num:int, callback:Callable[[int, int], str]):
    p: callback(int, int) = callback(num, 10)
    p.lower()
    
def executeCallback(num:int, callback:Callable[[None], None]):
    callback()

As in the example above, you can add information about a variable like a comment between the variable and =.

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
What is __init__.py?
Next Post
How to Use PyCharm with Mac