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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Jesús Espino
July 24, 2015
Programming
110
0
Share
Lightning Talk "Opening CPython Built-in Classes" (Europython)
Jesús Espino
July 24, 2015
More Decks by Jesús Espino
See All by Jesús Espino
Brewed for Speed: How Go’s Green Tea GC Works
jespino
0
18
Deep dive into the select statment
jespino
0
27
Exploring a complex codebase with AI
jespino
0
19
Explorando el Open Source con IA
jespino
0
18
Deep dive into a go binary
jespino
1
210
Deep dive in a SQL query (PgIbz)
jespino
2
150
Understanding the go runtime
jespino
1
180
Tests end-to-end en entornos complejos con testcontainers
jespino
0
55
Dissecting Slices, Maps and Channels in Go
jespino
0
85
Other Decks in Programming
See All in Programming
LM Linkで(非力な!)ノートPCでローカルLLM
seosoft
0
410
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
530
iOS機能開発のAI環境と起きた変化
ryunakayama
0
160
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
180
Rethinking API Platform Filters
vinceamstoutz
0
10k
Symfonyの特性(設計思想)を手軽に活かす特性(trait)
ickx
0
130
アーキテクチャモダナイゼーションとは何か
nwiizo
17
4.3k
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
120
「速くなった気がする」をデータで疑う
senleaf24
0
150
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.8k
モックわからないマン卒業記 ~振る舞いを起点に見直した、フロントエンドテストにおけるモックの使いどころ~
tasukuwatanabe
3
440
3分でわかるatama plusのQA/about atama plus QA
atamaplus
0
110
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
Building Applications with DynamoDB
mza
96
7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.6k
New Earth Scene 8
popppiees
2
2k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
450
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
210
A Soul's Torment
seathinner
5
2.6k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
エンジニアに許された特別な時間の終わり
watany
106
240k
Ethics towards AI in product and experience design
skipperchong
2
250
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
260
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