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
90
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
0
47
Deep dive in a SQL query (PgIbz)
jespino
2
72
Understanding the go runtime
jespino
0
79
Tests end-to-end en entornos complejos con testcontainers
jespino
0
19
Dissecting Slices, Maps and Channels in Go
jespino
0
40
Understanding the memory model of 5 languages
jespino
0
95
The Javascript Toolset
jespino
1
69
Deep dive in a SQL query
jespino
0
76
10 Aha! Moments reading the go source code
jespino
0
92
Other Decks in Programming
See All in Programming
CSC509 Lecture 09
javiergs
PRO
0
140
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
Outline View in SwiftUI
1024jp
1
330
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Featured
See All Featured
Speed Design
sergeychernyshev
25
620
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Typedesign – Prime Four
hannesfritz
40
2.4k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Done Done
chrislema
181
16k
A Philosophy of Restraint
colly
203
16k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Thoughts on Productivity
jonyablonski
67
4.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
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