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
0
94
型とは何か
CPython を実例に取り, 型がどのようなもので, どう機能するのかについて解説
cocoatomo
February 15, 2018
Tweet
Share
More Decks by cocoatomo
See All by cocoatomo
みんなの知らない翻訳の世界
cocoatomo
0
660
Sphinx/docutilsの光と闇
cocoatomo
1
740
PyPy における静的解析
cocoatomo
0
130
Sphinx が支える翻訳ドキュメント
cocoatomo
0
2.1k
Other Decks in Programming
See All in Programming
Compiling Python to WebAssembly with py2wasm
syrusakbary
0
140
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
hirobe1999
0
840
実践!RDRAを活用した既存システムの仕様変更 / Specification Changes in Existing Systems Utilizing RDRA
imamotohikaru
0
2.8k
Why 1 + 1 = 2 in Swift?
1plus4
1
250
Understanding Ast By Looking
inouehi
0
120
Parallel Socket Communication in Swift
s_shimotori
0
260
LLMチャットボットのアプリケーション設計Tips
os1ma
4
670
自作ソフト(VMagicMirror)がVRMA対応してる話+実装のTips
bakudreameater
0
110
ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php
okashoi
2
470
設計の知識と技能で駆動するソフトウェア開発
masuda220
PRO
19
11k
Enhancing Applications with Accessibility API
kishikawakatsumi
3
1k
document.write再考
brn
5
2.5k
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
319
20k
The Cost Of JavaScript in 2023
addyosmani
13
3.7k
10 Git Anti Patterns You Should be Aware of
lemiorhan
644
57k
Building a Scalable Design System with Sketch
lauravandoore
455
32k
We Have a Design System, Now What?
morganepeng
42
6.7k
Building Applications with DynamoDB
mza
88
5.6k
Being A Developer After 40
akosma
56
580k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
1.9k
The Invisible Side of Design
smashingmag
293
49k
What's in a price? How to price your products and services
michaelherold
236
11k
Fantastic passwords and where to find them - at NoRuKo
philnash
35
2.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
14
1.3k
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 ドキュメント翻訳中!