Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
静的型付け言語Python
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
utagawa kiki
July 14, 2016
Technology
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
静的型付け言語Python
KMC例会講座
utagawa kiki
July 14, 2016
More Decks by utagawa kiki
See All by utagawa kiki
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
2
1.3k
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
4.5k
tparseでgo testの出力を見やすくする
utgwkk
2
2.1k
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
4
2.6k
自動で //nolint を挿入する取り組み / Gopher's Gathering
utgwkk
1
3.3k
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
4.6k
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
21
17k
Dive into gomock / Go Conference 2024
utgwkk
14
8.8k
Goでリフレクションする、その前に / Kansai.go #1
utgwkk
4
3.8k
Other Decks in Technology
See All in Technology
強化学習「理論」入門
enakai00
3
3.6k
猫付きpingコマンドを自作
uyuki234
0
240
AWS ネットワーク構築でハマった(ハマりかけた) 5選とそこから得た教訓
nagisa53
4
240
トヨタ⽣産⽅式(TPS)⼊⾨
recruitengineers
PRO
3
890
関東Kaggler会発表資料
takoi
1
240
オートロックマンションなのに、各部屋は施錠なし!? 攻撃者が組織内ネットワークで大暴れする理由 / The Front Door Is Locked, but the Rooms Are Wide Open: Why Attackers Move Freely Inside Enterprise Networks
nttcom
1
6k
MulticaとPi Coding Agentで、小規模OSSを30本同時運用した流れ
eiei114
0
130
ラジオの科学
frievea
0
330
Head First モブプログラミング / Head First Mobprogramming
takaking22
10
12k
Apache Icebergインフラストラクチャ:ストレージ・カタログ・エンジンの選択肢とClouderaプラットフォームでの実装
tsugiyama
0
110
PLATEAU で バーチャル花火大会
tatsuya1970
0
160
QAエンジニア起点で進める、SmartHRにおける信頼性向上について
kaomi_wombat
1
140
Featured
See All Featured
Docker and Python
trallard
47
4.1k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
380
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
650
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
WENDY [Excerpt]
tessaabrams
11
39k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
Are puppies a ranking factor?
jonoalderson
1
3.8k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
490
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
410
Building a Scalable Design System with Sketch
lauravandoore
463
34k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
490
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
66
57k
Transcript
静的型付け言語Python
こんにちは • @utgw (うたがわ) • 工学部情報学科 計算機科学コース2回生 • 第39代副広報・root •
Twitter: @utgwkk • GitHub: @utgw
アンケートです • 動的型付け言語を書いたことがある • Python を書いたことがある • 型理論に詳しい
前置き • 型に関する踏み込んだ話はしません(できません) • あくまで言語機能の紹介としてやっていきます • Python 3 系の話をします ◦
Python 2 系は各位うまくやってくれ ◦ https://blog.ymyzk.com/2016/02/python-2-type-hints/ とかに載ってそう
Pythonの型ヒントの経緯 • 2006/12/2: PEP 3107 - Function Annotations ◦ Python
3.0 で関数の型ヒントだけ導入された ◦ あくまでもヒント • 2014/9/29: PEP 484 - Type Hints ◦ 変数の型ヒントやジェネリクスなど、より踏み込んだ型ヒン トに関する提案 • 2015/9/14: Python 3.5 リリース ◦ PEP 484 を実現するモジュール typing が導入された
動的型付け言語に静的型を導入する試み 1. TypeScript の場合 1.1. TypeScript のコードを書く 1.2. コンパイラが型チェックをする 1.3.
JavaScript にコンパイルされる 2. Python + 型ヒントの場合 2.1. Python のコードに型の情報を書く 2.2. 各位うまくやってくれ!!!!!
各位うまくやってくれ!!!!! • 各位うまくやっていきましょう ◦ 実行時に型チェックがされるわけではない ◦ そもそも言語としては動的型付け言語のまま ◦ むやみに互換性を壊したくない •
型チェックツールを使いましょう ◦ mypy を使いましょう ◦ そのうち PyCharm とかでも対応しそう
動的型付け言語に静的型を導入する試み(修正) 1. TypeScript の場合 1.1. TypeScript のコードを書く 1.2. コンパイラが型チェックをする 1.3.
JavaScript にコンパイルされる 2. Python + 型ヒントの場合 2.1. Python のコードに型の情報を書く 2.2. mypy 等で型チェックをする 2.3. よさそうだったら本番投入
従来のコード def fib(n): a, b = 0, 1 i =
0 while i <= n: yield a a, b = b, a + b i += 1
型情報のあるコード from typing import Iterator def fib(n: int) -> Iterator[int]:
a, b = 0, 1 i = 0 while i <= n: yield a a, b = b, a + b i += 1
型情報のあるコード from typing import Iterator def fib(n: int) -> Iterator[int]:
a, b = 0, 1 i = 0 while i <= n: yield a a, b = b, a + b i += 1
どのようにして型情報を記述するのか • ざっくり見ていきましょう ◦ 変数の型ヒント ◦ 関数の型ヒント ◦ cast ◦
Callable ◦ ジェネリクス ◦ 直和型 ◦ Optional ◦ Any ◦ その他
変数の型ヒント • 変数名 = 値 # type: 型名 • コメントに型名を記述することで型ヒントとする
• 型名の alias が定義できる ◦ 別名 = 型名
関数の型ヒント • def 関数名(x: 型, y: 型, ...) -> 返り値の型:
• 引数の既定値は x: 型 = 値 のように書く
cast • cast(キャストする型, 値) • 値をある型にキャストしていることを示す ◦ 実際にはキャストしない
Callable • Callable[[引数の型のリスト, ...], 返り値の型] • 呼び出し可能であることを示す
ジェネリクス • T = TypeVar(‘T’) • def hoge(val: T) ->
T: • ジェネリックなクラスが作れる
直和型 • Union[A, B, ...] • A, B, ...のいずれかの型であることを示す •
複数の型をとりうるが、ジェネリクスほどは自由でないときに使 う
Optional • Optional(X) = Union[X, None] • X もしくは None
であることを示す • 失敗するかもしれない操作に使う? ◦ それ例外処理でよくない?
Any • すべての型の親 • auto 型のような感じで使うのかな • 型ヒント書かなきゃいいじゃん
その他 • List[T]: T型のリスト • Dict[K, V]: K型をキーとしてV型の要素を格納する辞書型 • Iterable[T]:
T型の一連の要素を持つ型 • Container: 何らかのコンテナ • SupportsInt: int(x) という操作ができる型
実演
まとめ • Python は言語全体として静的型になるつもりはない • あくまで各位うまくやってくれ • mypy を使って型検査していこう ◦
まだうまくいってない部分もありそう? ◦ ぼくの型の指定がバグってたら許してくれ • 型理論よく分かってないので戸惑った
ありがとうございました • 質問など ◦ 型のことは型に詳しい人に聞いてくれ
参考 • typing — 型ヒントのサポート ◦ http://docs.python.jp/3/library/typing.html • PEP 484
◦ https://www.python.org/dev/peps/pep-0484/ • PEP 3107 ◦ https://www.python.org/dev/peps/pep-3107/ • mypy で静的型付け Python プログラミング ◦ http://t2y.hatenablog.jp/entry/2014/12/22/004525