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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
19
Deep dive into the select statment
jespino
0
28
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
160
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
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
200
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.9k
Getting more out of Maven
mlvandijk
0
110
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
200
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
4
870
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
3
270
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
390
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
240
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
340
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
120
AI時代のPhpStorm最新事情 #phpcon_odawara
yusuke
0
160
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
770
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1032
470k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
150
Evolving SEO for Evolving Search Engines
ryanjones
0
180
Mind Mapping
helmedeiros
PRO
1
150
Joys of Absence: A Defence of Solitary Play
codingconduct
1
340
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.3k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.5k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
370
Building Applications with DynamoDB
mza
96
7k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
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