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
96
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 a go binary
jespino
1
140
Deep dive in a SQL query (PgIbz)
jespino
2
91
Understanding the go runtime
jespino
1
120
Tests end-to-end en entornos complejos con testcontainers
jespino
0
28
Dissecting Slices, Maps and Channels in Go
jespino
0
58
Understanding the memory model of 5 languages
jespino
0
140
The Javascript Toolset
jespino
1
88
Deep dive in a SQL query
jespino
0
110
10 Aha! Moments reading the go source code
jespino
0
120
Other Decks in Programming
See All in Programming
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
1
120
GraphRAGの仕組みまるわかり
tosuri13
8
480
Systèmes distribués, pour le meilleur et pour le pire - BreizhCamp 2025 - Conférence
slecache
0
110
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
GoのGenericsによるslice操作との付き合い方
syumai
3
690
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
1
690
DroidKnights 2025 - 다양한 스크롤 뷰에서의 영상 재생
gaeun5744
3
320
Java on Azure で LangGraph!
kohei3110
0
170
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
190
ReadMoreTextView
fornewid
1
480
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
540
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
240
Featured
See All Featured
Practical Orchestrator
shlominoach
188
11k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
Adopting Sorbet at Scale
ufuk
77
9.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Designing for humans not robots
tammielis
253
25k
Embracing the Ebb and Flow
colly
86
4.7k
4 Signs Your Business is Dying
shpigford
184
22k
How STYLIGHT went responsive
nonsquared
100
5.6k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
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