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
Lightning Talk "Opening CPython Built-in Classe...
Search
Jesús Espino
July 24, 2015
Programming
0
97
Lightning Talk "Opening CPython Built-in Classes" (Europython)
Jesús Espino
July 24, 2015
Tweet
Share
More Decks by Jesús Espino
See All by Jesús Espino
Deep dive into the select statment
jespino
0
13
Exploring a complex codebase with AI
jespino
0
8
Explorando el Open Source con IA
jespino
0
7
Deep dive into a go binary
jespino
1
180
Deep dive in a SQL query (PgIbz)
jespino
2
120
Understanding the go runtime
jespino
1
140
Tests end-to-end en entornos complejos con testcontainers
jespino
0
34
Dissecting Slices, Maps and Channels in Go
jespino
0
66
Understanding the memory model of 5 languages
jespino
0
160
Other Decks in Programming
See All in Programming
TransformerからMCPまで(現代AIを理解するための羅針盤)
mickey_kubo
4
1.9k
SODA - FACT BOOK(JP)
sodainc
1
8.6k
Google Opalで使える37のライブラリ
mickey_kubo
3
130
Go言語の特性を活かした公式MCP SDKの設計
hond0413
1
410
実践Claude Code:20の失敗から学ぶAIペアプログラミング
takedatakashi
16
6.5k
pnpm に provenance のダウングレード を検出する PR を出してみた
ryo_manba
1
130
AIと人間の共創開発!OSSで試行錯誤した開発スタイル
mae616
2
770
AI Coding Meetup #3 - 導入セッション / ai-coding-meetup-3
izumin5210
0
3.4k
contribution to astral-sh/uv
shunsock
0
440
ソフトウェア設計の実践的な考え方
masuda220
PRO
4
630
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
290
CSC509 Lecture 06
javiergs
PRO
0
260
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Balancing Empowerment & Direction
lara
5
700
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
Scaling GitHub
holman
463
140k
Building Applications with DynamoDB
mza
96
6.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Typedesign – Prime Four
hannesfritz
42
2.8k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
Context Engineering - Making Every Token Count
addyosmani
7
280
Raft: Consensus for Rubyists
vanstee
140
7.2k
Thoughts on Productivity
jonyablonski
70
4.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Transcript
Opening CPython Built-in Classes JESUS ESPINO GARCIA, DEVELOPER #007000
What’s the problem?
Closed built-in classes >>> int.x = 3 Traceback (most recent
call last): File "<stdin>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'int'
The solution
Master Key >>> import masterkey >>> masterkey.open_class(int) >>> int.x =
3 >>> (10).x 3
Application
Fix broken float comparisons >>> assert 1.3332 == 1.3333 Traceback
(most recent call last): File "<stdin>", line 1, in <module> AssertionError >>> float.__eq__ = lambda self, other: abs(abs(self) - abs(other)) < 0.1 >>> 1.3332 == 1.3333 True >>> 1.7 == 1.1 False
Add sense to the << operator >>> 5 << 1
10 >>> 5 << 2 20 >>> int.__lshift__ = lambda self, other: self * (10 ** other) >>> 5 << 1 50 >>> 5 << 2 500
More semantic bool __repr__ >>> True == True True >>>
True == False False >>> bool.__repr__ = lambda x: ‘Yes’ if True else ‘No’ >>> True == True Yes >>> True == False No
Be more rubyist >>> from masterkey import rubint >>> (3).times(print)
0 1 2 >>> (3).upto(5, print) 4 5 >>> (3).downto(1, print) 2 1
Close your own classes >>> class MyClass: ... pass >>>
masterkey.close_class(MyClass) >>> MyClass.x = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'MyClass'
Of course, this is a joke
The black magic import ctypes from operator import xor longsize
= ctypes.sizeof(ctypes.c_long) def open_class(klass): int_flags = ctypes.c_long.from_address(id(klass) + longsize * 21) int_flags.value |= 1 << 9 def close_class(klass): int_flags = ctypes.c_long.from_address(id(klass) + longsize * 21) int_flags.value = xor(int_flags.value, 1 << 9)
The code http://www.github.com/jespino/python-master-key