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
Pythonを読む
Search
tera
August 27, 2017
Technology
0
220
Pythonを読む
第9回 HojiroLTで話したスライドです。
tera
August 27, 2017
Tweet
Share
More Decks by tera
See All by tera
ピッてやるとシュッてなるやつを作った話
tera
0
260
importを理解したかった
tera
0
650
RustでShell作ろう
tera
0
920
HTTPをしゃべろう~Firefoxになるために~
tera
0
220
Other Decks in Technology
See All in Technology
Prox Industries株式会社 会社紹介資料
proxindustries
0
270
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
180
2年でここまで成長!AWSで育てたAI Slack botの軌跡
iwamot
PRO
4
670
Snowflake Summit 2025 データエンジニアリング関連新機能紹介 / Snowflake Summit 2025 What's New about Data Engineering
tiltmax3
0
300
Model Mondays S2E02: Model Context Protocol
nitya
0
220
Абьюзим random_bytes(). Фёдор Кулаков, разработчик Lamoda Tech
lamodatech
0
330
How Community Opened Global Doors
hiroramos4
PRO
1
110
AIエージェント最前線! Amazon Bedrock、Amazon Q、そしてMCPを使いこなそう
minorun365
PRO
13
4.9k
A2Aのクライアントを自作する
rynsuke
1
170
Welcome to the LLM Club
koic
0
160
SalesforceArchitectGroupOsaka#20_CNX'25_Report
atomica7sei
0
140
GeminiとNotebookLMによる金融実務の業務革新
abenben
0
220
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Facilitating Awesome Meetings
lara
54
6.4k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Agile that works and the tools we love
rasmusluckow
329
21k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Rebuilding a faster, lazier Slack
samanthasiow
81
9.1k
Transcript
Pythonを読む
自己紹介 https://tera3939.github.io/
お話の流れ • CPythonのソースに挑む • 一旦C拡張について眺めてみる • もう一回挑んでみる
CPythonのソースに挑む
CPythonとは • Pythonの処理系には様々な種類がある ◦ pypy ◦ IronPython ◦ etc... •
その中でも最も有名なのがCPython • C言語で実装されている • ”Python”とだけかかれている場合は概ねCPythonのことを指す
ソースコードを手に入れる • GitHubからcloneしてくる ◦ git clone https://github.com/python/cpython.git • ディレクトリ内はこんな感じ
方針を立てる • 今回はビルトインのintについて見にいきたい • でも、どこを読めばいい?
intの場所を探す • Python Developer’s Guide 24.1 ◦ https://docs.python.org/devguide/exploring.html • ここにCPythonのソースのレイアウトが書いてある
ソースを眺める • Object/intobject.cとかある? → 無い…… • とりあえずPython/bltinmodule.cを眺めてみる
None
それっぽいのはあるけど…… • よくわかんない
C拡張について眺めてみる
CによるPythonの拡張 • Pythonインタプリタの拡張と埋め込み ◦ https://docs.python.jp/3/extending/index.html • Python/C API リファレンスマニュアル ◦
https://docs.python.jp/3/c-api/index.html
モジュール作成に必要な関数・構造体 • PyMethodDefの配列 ◦ メソッドテーブル ◦ メソッド名とメソッドへの関数ポインタ • PyModuleDef ◦
モジュール名、ドキュメント、メソッドテーブルの参照を持つ • PyMODINIT_FUNC PyInit_<modulename> ◦ <modulename> == PyModuleDefで定義したメソッド名 ◦ 事実上の__init__.py
型作成に必要な構造体 • 型自体の構造体 ◦ フィールドを持つ • PyTypeObject ◦ 型名、__hoge__メソッドとなる関数のポインタを持つ •
PyModuleDef • PyInit_<modulename> ◦ このInitがいつ呼ばれるかは謎 (たすけて) ◦ たぶんインポートされるとき ……
これさえ分かればどうにかなる!(???)
もう一度CPythonのソースに挑む
読んだ!
結果 • intはObjects/longobject.cに書いてある • -5~256の範囲の整数はsmall_intsという配列にキャッシュされている • その範囲内の整数は対応するsmall_intsのポインタを返す • そのためis演算子でその範囲の整数を比較するとTrueを返す ◦
a = 1; b = 1; a is b # -> True ◦ a = 500; b = 500; a is b # False
おわり