Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
660
RustでShell作ろう
tera
0
930
HTTPをしゃべろう~Firefoxになるために~
tera
0
240
Other Decks in Technology
See All in Technology
202512_AIoT.pdf
iotcomjpadmin
0
150
AWSに革命を起こすかもしれない新サービス・アップデートについてのお話
yama3133
0
510
TED_modeki_共創ラボ_20251203.pdf
iotcomjpadmin
0
150
Amazon Connect アップデート! AIエージェントにMCPツールを設定してみた!
ysuzuki
0
140
日本Rubyの会: これまでとこれから
snoozer05
PRO
6
240
Kiro を用いたペアプロのススメ
taikis
4
1.9k
Strands Agents × インタリーブ思考 で変わるAIエージェント設計 / Strands Agents x Interleaved Thinking AI Agents
takanorig
5
2.1k
Connection-based OAuthから学ぶOAuth for AI Agents
flatt_security
0
380
Entity Framework Core におけるIN句クエリ最適化について
htkym
0
130
半年で、AIゼロ知識から AI中心開発組織の変革担当に至るまで
rfdnxbro
0
150
子育てで想像してなかった「見えないダメージ」 / Unforeseen "hidden burdens" of raising children.
pauli
2
330
_第4回__AIxIoTビジネス共創ラボ紹介資料_20251203.pdf
iotcomjpadmin
0
140
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
From π to Pie charts
rasagy
0
92
Deep Space Network (abreviated)
tonyrice
0
22
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
170
HDC tutorial
michielstock
0
280
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
110
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
210
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
270
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
0
22
The Curious Case for Waylosing
cassininazir
0
190
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
おわり