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
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
型とは何か
CPython を実例に取り, 型がどのようなもので, どう機能するのかについて解説
cocoatomo
February 15, 2018
More Decks by cocoatomo
See All by cocoatomo
みんなの知らない翻訳の世界
cocoatomo
0
830
Sphinx/docutilsの光と闇
cocoatomo
1
890
PyPy における静的解析
cocoatomo
0
160
Sphinx が支える翻訳ドキュメント
cocoatomo
0
2.9k
Other Decks in Programming
See All in Programming
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
170
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
150
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
120
仕様駆動開発の消費期限
watany
20
9k
源内ハンズオン概要編
hideg
0
220
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
500
Introducing Stack Pull Request in GitHub
kkamegawa
0
110
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.8k
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
260
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
530
My Marp Sample
sinoue0108
0
120
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
280
Featured
See All Featured
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
360
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
580
4 Signs Your Business is Dying
shpigford
187
23k
The Curious Case for Waylosing
cassininazir
1
480
From π to Pie charts
rasagy
0
330
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
420
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Building Adaptive Systems
keathley
44
3.2k
Deep Space Network (abreviated)
tonyrice
0
270
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
380
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 ドキュメント翻訳中!