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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
Let's Change Our World—Again
snoozer05
PRO
6
670
VLA-JEPA: Enhancing Vision-Language-Action Models with Latent World Models
takmin
0
320
Temporal 最高!でも改めて「日付」について調べたら人類の負債すぎる件
riyaamemiya
0
230
企画を Software Factory-ready にする——開発が高速になったとき、PdMの仕事はどう変わったか
yachibit
1
260
「エーアイ」をバズワードにしないために RPA 導入経験と Kiro 上での Claude モデル利用経験から
kazzpapa3
0
120
スクラム祭り2026福井トラック_閉じたループから価値のスパイラルへ
kc_nakanishi
0
140
DevelopersIO2026 -バックアップは“消されないこと”が9割-
yoshida0129
0
400
Vue.jsのGitHubから学ぶ意思決定
yut0naga1_fa
1
150
[EMNLP26] RIGEL: Self-Distilled Score Adaptation for Image and Video Captioning Evaluation
keio_smilab
PRO
0
120
Claude for Google Workspaces ポン出し/claude-for-google-workspace-slide-conversion
emiki
0
13k
BacklogのRAG 今ならこう作る - S3 Vectorsと自前パイプラインの勘所
revsystem
0
130
ストレスなくAIと付き合いたい!こんなときどうしてる?
namiki64
4
690
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
350
Building Adaptive Systems
keathley
44
3.3k
Writing Fast Ruby
sferik
630
63k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
Building the Perfect Custom Keyboard
takai
2
920
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
How to train your dragon (web standard)
notwaldorf
96
6.9k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
530
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2.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
おわり