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 Classes" (Europython)
Search
Jesús Espino
July 24, 2015
Programming
0
75
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
Dissecting Slices, Maps and Channels in Go
jespino
0
4
Understanding the memory model of 5 languages
jespino
0
34
The Javascript Toolset
jespino
0
16
Deep dive in a SQL query
jespino
0
29
10 Aha! Moments reading the go source code
jespino
0
35
The secret life of a goroutine (Fosdem 2024)
jespino
0
32
Understanding the go runtime (Golab)
jespino
0
120
Understanding the Go Compiler (Gophercon UK 23)
jespino
0
74
Understanding the Go Compiler
jespino
0
46
Other Decks in Programming
See All in Programming
Apache Hive 4 on Treasure Data
ryukobayashi
0
250
SIMD Parallel Programming with the Vector API
josepaumard
0
170
雑に思考を整理する技術と効能
konifar
59
29k
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
0
550
Ruby Function Composition
bkuhlmann
1
330
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
270
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
270
Elm Form Validation
bkuhlmann
0
510
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
1
160
Kotlin Multiplatform at Stable and Beyond (Android Makers 2024)
zsmb
0
190
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Six Lessons from altMBA
skipperchong
21
3k
Optimizing for Happiness
mojombo
370
69k
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.5k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
How GitHub (no longer) Works
holman
304
140k
Building Applications with DynamoDB
mza
88
5.6k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Typedesign – Prime Four
hannesfritz
36
2.1k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
Building Your Own Lightsaber
phodgson
99
5.7k
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