関数アノテーション

投稿者: | 2022-12-31

https://peps.python.org/pep-3107/

  • 型ヒントとも呼ばれる
  • 多くのプログラミング言語では静的型付け、Pythonは動的型付け
  • そのためPythonは変数、パラメーター、戻り値はどんな型でも設定ができ、プログラムの実行中にデータ型を変更することができる。そのため形式的な指定が少なくすむ反面、バグが潜みやすい
  • 設定すればわかりやすくなるが、これによって型チェックをおこなってくれるわけではない
  • 型チェックは外部ツールを導入すればできるようになる
def calc(a:int):
# int, str, float, listなど

def calc(a:int)->int:

def calc(a:int)->None:

def calc(a: 'description'):

# デフォルト引数を付けるときはアノテーションの後
def calc(a:int=5):

def calc(a:int,
         b:int,
         c:str):

def compile(source: "something compilable",
            filename: "where the compilable thing comes from",
            mode: "is this a single statement or a suite?"):
    ...