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
型とは何か
Search
cocoatomo
February 15, 2018
Programming
120
0
Share
型とは何か
CPython を実例に取り, 型がどのようなもので, どう機能するのかについて解説
cocoatomo
February 15, 2018
More Decks by cocoatomo
See All by cocoatomo
みんなの知らない翻訳の世界
cocoatomo
0
820
Sphinx/docutilsの光と闇
cocoatomo
1
870
PyPy における静的解析
cocoatomo
0
160
Sphinx が支える翻訳ドキュメント
cocoatomo
0
2.8k
Other Decks in Programming
See All in Programming
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
430
AIとRubyの静的型付け
ukin0k0
0
490
Old Dog, New Tricks: The Java 25 Reinvention - JNation
bazlur_rahman
0
140
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
520
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
520
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
1.2k
AI開発を加速するためにテスト戦略を言語化した
yoshihiro_shu
0
110
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
180
net-httpのHTTP/2対応について
naruse
0
400
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.4k
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
330
SPMマルチモジュールで テストカバレッジを取得する技法
yosshi4486
0
140
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
180
Music & Morning Musume
bryan
47
7.2k
The SEO Collaboration Effect
kristinabergwall1
1
470
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Prompt Engineering for Job Search
mfonobong
0
320
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
30 Presentation Tips
portentint
PRO
1
310
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
150
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
300
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Transcript
型とは何か? CPython の実装 by @cocoatomo, written in HuiFontP
ご結婚 おめでとうございます!! • 型い愛で結ばれたお二人に • CPython の型のお話をプレゼント
そもそも型とは? • 例を出すと
例えば血液型 • A型 • B型 • AB型 • O型
例えば軍艦 • 金剛型 • 陽炎型 • 天城型 • 利根型 •
etc…
「型とは値の集合である」 • Types and Programming Languages • ↑型についての入門書 • 「名古屋入国のためのパスポート」
• 安全性 = 推進性 + 保全性
何の役に立つの? • 推進性 ⇒ 式の評価 (=計算) が値にな るまで実行できることが分かる • 保全性
⇒ 式の評価によって型が変わら ない • ⇒ 「ある程度」ちゃんと動くことが保証さ れ, 結果の予想できるプログラムが書ける
理論は分かった では実際の実装は? • CPythonを見てみよう
え? Python に型あるの?? • 「動的言語だから型は弱いんじゃないの?」 • → その発言は危険が危ない!
Pythonにだって型はある ! ! ! • TypeError: 型のエラー
混ぜるな危険 • 「型付けルールがある」=型付き言語 • 「値の型」と「変数の型」 • ⇒ Python に無いのは変数の型 •
「型チェックが『実行する』前に or 後で行 われる」=(いわゆる) 静的とか動的とか • [余談] ところで「実行」の定義, 考えた ことありますか? (読者の宿題)
Pythonとは • クラスベースオブジェクト指向プログラミング 言語 (全てのものはobject) • 実行時の型チェック • 参照カウントGC •
思想: Zen of Python • Explicit is better than implicit. etc…
CPythonとは • C で実装された Python • ⇒ C とは異なる型システムをどう実装し ているのか?
ソースコード • https://www.python.org/ftp/python/ 3.4.0/Python-3.4.0.tar.xz にソースコー ドがある. • ↑のリンクがあるページは https:// www.python.org/downloads/release/
python-340/
PyObject • Pythonの型階層の基底クラス object の C 実装 • 「全てのものはPyObject!」 •
Include/object.h で宣言されている型 • ⇒ 実装を見てみよう
PyObjectの実装 • 実体はCの「構造体」
PyObject の型は? ⇒ ob_type が PyObject の型
struct _typeobject とは? ⇒ CPythonの型は「構造体」で表現!
その中身は? ずらっと関数ポインタが並ぶ
PyTypeObject 最後に PyTypeObject に typedef
CPythonの型 • 結局, CPythonにとっての型は関数の集ま り (構造体) • (→他の言語での型の表現方法について は詳しい人教えて)
型の判定 • メソッドの引数チェックとかどうしてるの? • → さっきの「3 +’a’」の C での動きを見 てみよう
メソッド呼び出しとは? • Python では以下の3つは同じ意味 • 「3 + ’a’」 • =「3.__add__(‘a’)」
• =「int.__add__(3, ‘a’)」
C では? • 「int.__add__」の実装は • Object/abstract.c • にある • PyNumber_Add
関数
CPython の読み方 • CPython の命名規則ではメソッドの実 装は • Object/<type name>object.c •
にある • Py<type name>_<method name> • という名前の関数となっている
Number 型って? • int, float, bool, complex の上位の 抽象型 •
Python の protocol と呼ばれるもの
protocol • Java の interface のようなもの • 実装すべきメソッドの集合を定めてある • Long
は Number protocol を提供して いる • もちろんコンパイラのチェックは無い • ↑そもそも Python にコンパイラが無い
結局, 呼び出されるのは • PyNumber_Add から, • PyLong_Type->tp_as_number ->nb_add • と型構造体のメンバを辿って
• Object/longobject.c • にある long_add 関数が呼ばれる
long_add 関数 ⇒ CHECK_BINOP に注目!
CHECK_BINOP macro ⇒ ここで引数チェックをしていた!
なんで型チェックするの? • long_add の引数は PyLongObject なの に、なんで CHECK_BINOP で型チェックす るの!?
long_add の型への登録 • PyLong_Type->tp_as_number のメンバ nb_add に long_add が代入される箇所 を見ると……
キャストされとる! (※この long_as_number が PyLong_Type->tp_as_number に代入される)
PyObject だらけ • 実行時に PyObject から PyTypeObject (=関数の集まり) を取得し, 最初に型チェッ
ク • よく見ると, せっかくの C の型情報がキャ ストで消えてる (引数も返り値も PyObject) • ⇒ これが「動的」と言われる所以
Python/C API • ここらへんの内容は, Python 公式ドキュ メントの C API の項に載ってる
• http://docs.python.jp/3.3/c-api/ index.html
公式ドキュメント • 言語仕様, チュートリアル, 標準ライブラリ の解説……が載っている文書 • 日本語訳もあるよ • 私を含めチームで3.3を翻訳中
• ⇒ I needs you • https://code.google.com/p/python- doc-ja/
まとめ • CPython の型オブジェクトは構造体 • CPython の実行時の型チェックは、型オ ブジェクトを毎回調べる • Python
3.3 ドキュメント翻訳中!