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
Meta-programming in Python
Search
Daniël Franke
August 08, 2013
Technology
0
190
Meta-programming in Python
A lightning talk I did for the Amsterdam Python Meetup group about meta-programming in Python.
Daniël Franke
August 08, 2013
Tweet
Share
Other Decks in Technology
See All in Technology
dbt開発 with Claude Codeのためのガードレール設計
10xinc
2
1.2k
250905 大吉祥寺.pm 2025 前夜祭 「プログラミングに出会って20年、『今』が1番楽しい」
msykd
PRO
1
950
Django's GeneratedField by example - DjangoCon US 2025
pauloxnet
0
150
Language Update: Java
skrb
2
300
AIエージェント開発用SDKとローカルLLMをLINE Botと組み合わせてみた / LINEを使ったLT大会 #14
you
PRO
0
120
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
8.7k
roppongirb_20250911
igaiga
1
240
La gouvernance territoriale des données grâce à la plateforme Terreze
bluehats
0
180
5年目から始める Vue3 サイト改善 #frontendo
tacck
PRO
3
220
【NoMapsTECH 2025】AI Edge Computing Workshop
akit37
0
200
Webアプリケーションにオブザーバビリティを実装するRust入門ガイド
nwiizo
7
840
Android Audio: Beyond Winning On It
atsushieno
0
850
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
525
40k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
A designer walks into a library…
pauljervisheath
207
24k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Optimizing for Happiness
mojombo
379
70k
Building an army of robots
kneath
306
46k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Fireside Chat
paigeccino
39
3.6k
How to Ace a Technical Interview
jacobian
279
23k
Transcript
Meta-programming in Python Daniël Franke <
[email protected]
> @ainmosni - http://ams-sec.org
What?
What? • Objects are instances of classes
What? • Objects are instances of classes • Classes are
Objects as well
What? • Objects are instances of classes • Classes are
Objects as well • What are Classes instances of?
Classes are objects In [1]: class Spam(object): ...: pass ...:
In [2]: type(Spam)
Classes are objects In [1]: class Spam(object): ...: pass ...:
In [2]: type(Spam) Out[2]: type
type: Creating the creators In [1]: Spam = type('Spam', (object,),
{'canned': True})
type: Creating the creators In [1]: Spam = type('Spam', (object,),
{'canned': True}) In [2]: type(Spam) Out[2]: type
type: Creating the creators In [1]: Spam = type('Spam', (object,),
{'canned': True}) In [2]: type(Spam) Out[2]: type In [3]: canned_spam = Spam() In [4]: type(canned_spam) Out[4]: __main__.Spam
type: Creating the creators In [1]: Spam = type('Spam', (object,),
{'canned': True}) In [2]: type(Spam) Out[2]: type In [3]: canned_spam = Spam() In [4]: type(canned_spam) Out[4]: __main__.Spam In [5]: canned_spam.canned Out[5]: True In [6]: Spam.canned Out[6]: True
A simple metaclass In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: cls_dict['canned'] = ‘Metacan’ ...: return type.__new__(metaclass, name, bases, cls_dict) ...:
A simple metaclass In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: cls_dict['canned'] = ‘Metacan’ ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [2]: class Spam(object): ...: __metaclass__ = ismeta ...: pass ...:
A simple metaclass In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: cls_dict['canned'] = ‘Metacan’ ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [2]: class Spam(object): ...: __metaclass__ = ismeta ...: pass ...: In [3]: Spam.canned Out[3]: 'Metacan'
Reading class properties In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: for key, value in cls_dict.items(): ...: print('{}: {}'.format(key, value)) ...: return type.__new__(metaclass, name, bases, cls_dict) ...:
Reading class properties In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: for key, value in cls_dict.items(): ...: print('{}: {}'.format(key, value)) ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [2]: class Spam(object): ...: __metaclass__ = ismeta ...: def hello(self): ...: print('Hello byte') ...:
Reading class properties In [1]: class ismeta(type): ...: def __new__(metaclass,
name, bases, cls_dict): ...: for key, value in cls_dict.items(): ...: print('{}: {}'.format(key, value)) ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [2]: class Spam(object): ...: __metaclass__ = ismeta ...: def hello(self): ...: print('Hello byte') ...: __module__: __main__ __metaclass__: <class '__main__.ismeta'> hello: <function hello at 0x2acf140>
Adding methods via the metaclass In [1]: def hello(self): ...:
print('Hello byte!') ...:
Adding methods via the metaclass In [1]: def hello(self): ...:
print('Hello byte!') ...: In [2]: class ismeta(type): ...: def __new__(metaclass, name, bases, cls_dict): ...: cls_dict['hi'] = hello ...: return type.__new__(metaclass, name, bases, cls_dict) ...:
Adding methods via the metaclass In [1]: def hello(self): ...:
print('Hello byte!') ...: In [2]: class ismeta(type): ...: def __new__(metaclass, name, bases, cls_dict): ...: cls_dict['hi'] = hello ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [3]: class Spam(object): ...: __metaclass__ = ismeta ...: pass ...:
Adding methods via the metaclass In [1]: def hello(self): ...:
print('Hello byte!') ...: In [2]: class ismeta(type): ...: def __new__(metaclass, name, bases, cls_dict): ...: cls_dict['hi'] = hello ...: return type.__new__(metaclass, name, bases, cls_dict) ...: In [3]: class Spam(object): ...: __metaclass__ = ismeta ...: pass ...: In [4]: Spam().hi() Hello byte!
Why?
Why? • “Self aware” class methods
Why? • “Self aware” class methods • Bookkeeping
Why? • “Self aware” class methods • Bookkeeping • Slots
Why? • “Self aware” class methods • Bookkeeping • Slots
• Syntactic sugar
Why? • “Self aware” class methods • Bookkeeping • Slots
• Syntactic sugar • Obfuscation
A “practical” example In [1]: spam_types = {} In [2]:
class ismeta(type): ...: def __new__(metaclass, name, bases, cls_dict): ...: new_class = type.__new__(metaclass, name, ...: bases, cls_dict) ...: if not attr_dict.get('abstract', False): ...: spam_types[name] = new_class ...: return new_class In [3]: class Spam(object): ...: __metaclass__ = ismeta ...: abstract = True In [4]: class CannedSpam(Spam): ...: pass
A “practical” example In [5]: class EmailSpam(Spam): ...: pass ...:
In [6]: spam_types Out[6]: {'CannedSpam': __main__.CannedSpam, 'EmailSpam': __main__. EmailSpam}
Thank you Any questions?