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
0
110
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
Brewed for Speed: How Go’s Green Tea GC Works
jespino
0
10
Deep dive into the select statment
jespino
0
22
Exploring a complex codebase with AI
jespino
0
12
Explorando el Open Source con IA
jespino
0
11
Deep dive into a go binary
jespino
1
190
Deep dive in a SQL query (PgIbz)
jespino
2
140
Understanding the go runtime
jespino
1
170
Tests end-to-end en entornos complejos con testcontainers
jespino
0
50
Dissecting Slices, Maps and Channels in Go
jespino
0
79
Other Decks in Programming
See All in Programming
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
CSC307 Lecture 01
javiergs
PRO
0
690
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
120
Architectural Extensions
denyspoltorak
0
280
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
600
Oxlint JS plugins
kazupon
1
860
CSC307 Lecture 09
javiergs
PRO
1
830
Featured
See All Featured
Ruling the World: When Life Gets Gamed
codingconduct
0
140
Discover your Explorer Soul
emna__ayadi
2
1.1k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
RailsConf 2023
tenderlove
30
1.3k
New Earth Scene 8
popppiees
1
1.5k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
580
Rails Girls Zürich Keynote
gr2m
96
14k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
250
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
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