Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Pythonを読む
Search
tera
August 27, 2017
Technology
230
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pythonを読む
第9回 HojiroLTで話したスライドです。
tera
August 27, 2017
More Decks by tera
See All by tera
ピッてやるとシュッてなるやつを作った話
tera
0
270
importを理解したかった
tera
0
670
RustでShell作ろう
tera
0
970
HTTPをしゃべろう~Firefoxになるために~
tera
0
270
Other Decks in Technology
See All in Technology
How to Design Configuration for Rails Applications
willnet
1
830
WasmをRustにする
sansantech
PRO
0
260
DWH の限界突破! AI エージェント向け爆速リアルタイムデータ基盤 ClickHouse
jozono
1
490
パスキーの自動テストはどこまでいけるのか
teamlab
PRO
0
270
SAENM ~Jupyterで残すPoCの試行錯誤、未来の自分と仲間を助ける記録!~
terapyon
0
110
アウトプット初心者、半年間のふりかえり
4o1sobblue
0
440
20261017福岡devIO 「AIは"良いダッシュボード"を作れるか?
jtakeda
0
180
hackers_champloo_2026_tidb
makocchi
0
170
AI時代のPRレビューにどう向き合うか?
kairim0
1
240
Persisting Massive Data with Rails ~Designing for 60 billion rows, and fighting the bottlenecks~ Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
0
730
Benchmarking Disaggregated Serving on vLLM + Dynamo with a Real Coding-Agent Workload
nttcom
1
200
会社説明資料
persolserverworks
0
9.8k
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
550
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
67k
Side Projects
sachag
456
43k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
600
Designing Powerful Visuals for Engaging Learning
tmiket
1
610
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
おわり