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
実践! AIエージェント導入記
1mono2prod
0
150
BigQuery Remote FunctionでLooker Studioをインタラクティブ化
cuebic9bic
3
260
Uniadex__公開版_20250617-AIxIoTビジネス共創ラボ_ツナガルチカラ_.pdf
iotcomjpadmin
0
160
Prox Industries株式会社 会社紹介資料
proxindustries
0
260
20250625 Snowflake Summit 2025活用事例 レポート / Nowcast Snowflake Summit 2025 Case Study Report
kkuv
1
290
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
210
5min GuardDuty Extended Threat Detection EKS
takakuni
0
110
mrubyと micro-ROSが繋ぐロボットの世界
kishima
2
110
データプラットフォーム技術におけるメダリオンアーキテクチャという考え方/DataPlatformWithMedallionArchitecture
smdmts
5
610
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
170
AWS CDK 実践的アプローチ N選 / aws-cdk-practical-approaches
gotok365
6
680
CI/CD/IaC 久々に0から環境を作ったらこうなりました
kaz29
1
150
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
BBQ
matthewcrist
89
9.7k
Designing for Performance
lara
609
69k
Adopting Sorbet at Scale
ufuk
77
9.4k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Gamification - CAS2011
davidbonilla
81
5.3k
Fireside Chat
paigeccino
37
3.5k
How GitHub (no longer) Works
holman
314
140k
Writing Fast Ruby
sferik
628
61k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Why Our Code Smells
bkeepers
PRO
337
57k
We Have a Design System, Now What?
morganepeng
53
7.7k
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?