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
100
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
14
Exploring a complex codebase with AI
jespino
0
9
Explorando el Open Source con IA
jespino
0
8
Deep dive into a go binary
jespino
1
180
Deep dive in a SQL query (PgIbz)
jespino
2
130
Understanding the go runtime
jespino
1
150
Tests end-to-end en entornos complejos con testcontainers
jespino
0
35
Dissecting Slices, Maps and Channels in Go
jespino
0
70
Understanding the memory model of 5 languages
jespino
0
160
Other Decks in Programming
See All in Programming
例外処理を理解して、設計段階からエラーを見つけやすく、起こりにくく #phpconfuk
kajitack
12
6.3k
The Missing Link in Angular's Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
140
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
220
2026年向け会社紹介資料
misu
0
250
Amazon Bedrock Knowledge Bases Hands-on
konny0311
0
150
モデル駆動設計をやってみよう Modeling Forum2025ワークショップ/Let’s Try Model-Driven Design
haru860
0
170
目的で駆動する、AI時代のアーキテクチャ設計 / purpose-driven-architecture
minodriven
9
2.9k
モビリティSaaSにおけるデータ利活用の発展
nealle
0
540
イベントストーミングのはじめかた / Getting Started with Event Storming
nrslib
1
650
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.2k
「正規表現をつくる」をつくる / make "make regex"
makenowjust
1
680
チーム開発の “地ならし"
konifar
8
5.6k
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
697
190k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Being A Developer After 40
akosma
91
590k
Documentation Writing (for coders)
carmenintech
76
5.1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
What's in a price? How to price your products and services
michaelherold
246
12k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Statistics for Hackers
jakevdp
799
230k
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