0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python関数アノテーション(型ヒント)

0
Posted at

関数アノテーションとは

関数アノテーション(関数の型ヒント) は、Python の関数に「引数や戻り値の型をコメント的に書ける仕組み」。

基本の書き方

def 関数名(引数名: , 引数名2:  = デフォルト値) -> 戻り値の型:
    処理

def add(x:int,y:int) -> int:
    return x + y

👉 この関数は「整数 x と y を受け取り、整数を返す」ことを表す。

関数アノテーションのメリット

・コードの可読性が上がる
→ 他人が見ても「どんな型を期待してるのか」分かりやすい
・IDEの補完が効きやすくなる
→ VSCode / PyCharm で型補完が出やすい
・静的解析ツールでミスを事前検出
→ 実行前に「型が違う」と警告が出せる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?