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
Radical Imagining - LIFT 2025-2027 Policy Agenda
lift1998
0
370
forteeの改修から振り返るPHPerKaigi 2026
muno92
PRO
3
290
Making the RBS Parser Faster
soutaro
0
410
ルールルルルルRubyの中身の予備知識 ── RubyKaigiの前に予習しなイカ?
ydah
1
190
Coding as Prompting Since 2025
ragingwind
0
840
(Re)make Regexp in Ruby: Democratizing internals for the JIT
makenowjust
2
190
「Linuxサーバー構築標準教科書」を読んでみた #ツナギメオフライン.7
akase244
0
1.4k
Angular Signal Forms
debug_mode
0
110
Programming with a DJ Controller — not vibe coding
m_seki
3
110
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
550
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
おれのAgentic Coding 2026/03
tsukasagr
1
150
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.6k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
220
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
680
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
410
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
270
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 ドキュメント翻訳中!