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
Dylib Hijacking on macOS: Dead or Alive?
patrickwardle
0
370
『バイトル』CTOが語る! AIネイティブ世代と切り拓くモノづくり組織
dip_tech
PRO
1
130
LLMアプリの地上戦開発計画と運用実践 / 2025.10.15 GPU UNITE 2025
smiyawaki0820
1
650
Introduction to Bill One Development Engineer
sansan33
PRO
0
300
Biz職でもDifyでできる! 「触らないAIワークフロー」を実現する方法
igarashikana
2
670
HR Force における DWH の併用事例 ~ サービス基盤としての BigQuery / 分析基盤としての Snowflake ~@Cross Data Platforms Meetup #2「BigQueryと愉快な仲間たち」
ryo_suzuki
0
240
Liquid AI Hackathon Tokyo プレゼン資料
aratako
0
110
ニッポンの人に知ってもらいたいGISスポット
sakaik
0
170
プレーリーカードを活用しよう❗❗デジタル名刺交換からはじまるイベント会場交流のススメ
tsukaman
0
180
Node.js 2025: What's new and what's next
ruyadorno
0
570
能登半島地震で見えた災害対応の課題と組織変革の重要性
ditccsugii
0
1k
サイバーエージェント流クラウドコスト削減施策「みんなで金塊堀太郎」
kurochan
4
2.1k
Featured
See All Featured
Gamification - CAS2011
davidbonilla
81
5.5k
How GitHub (no longer) Works
holman
315
140k
Rails Girls Zürich Keynote
gr2m
95
14k
KATA
mclloyd
PRO
32
15k
How to Ace a Technical Interview
jacobian
280
24k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Agile that works and the tools we love
rasmusluckow
331
21k
BBQ
matthewcrist
89
9.8k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Done Done
chrislema
185
16k
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?