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
110
型とは何か
CPython を実例に取り, 型がどのようなもので, どう機能するのかについて解説
cocoatomo
February 15, 2018
Tweet
Share
More Decks by cocoatomo
See All by cocoatomo
みんなの知らない翻訳の世界
cocoatomo
0
790
Sphinx/docutilsの光と闇
cocoatomo
1
850
PyPy における静的解析
cocoatomo
0
150
Sphinx が支える翻訳ドキュメント
cocoatomo
0
2.7k
Other Decks in Programming
See All in Programming
問題の見方を変える「システム思考」超入門
panda_program
0
160
テーブル定義書の構造化抽出して、生成AIでDWH分析を試してみた / devio2025tokyo
kasacchiful
0
390
Dive into Triton Internals
appleparan
0
450
AI Agent 時代的開發者生存指南
eddie
4
2.3k
ネストしたdata classの面倒な更新にさようなら!Lensを作って理解するArrowのOpticsの世界
shiita0903
1
280
Introducing RemoteCompose: break your UI out of the app sandbox.
camaelon
2
470
釣り地図SNSにおける有料機能の実装
nokonoko1203
0
210
外接に惑わされない自システムの処理時間SLIをOpenTelemetryで実現した話
kotaro7750
0
210
GC25 Recap: The Code You Reviewed is Not the Code You Built / #newt_gophercon_tour
mazrean
0
150
Amazon ECS Managed Instances が リリースされた!キャッチアップしよう!! / Let's catch up Amazon ECS Managed Instances
cocoeyes02
0
130
퇴근 후 1억이 거래되는 서비스 만들기 | 내가 AI를 사용하는 방법
maryang
2
480
チームのテスト力を総合的に鍛えてシフトレフトを推進する/Shifting Left with Software Testing Improvements
goyoki
4
2k
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
Optimizing for Happiness
mojombo
379
70k
Facilitating Awesome Meetings
lara
57
6.6k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
660
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Designing for humans not robots
tammielis
254
26k
Visualization
eitanlees
150
16k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
2
290
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.2k
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 ドキュメント翻訳中!