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
180
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
DevOpsDays History and my DevOps story
kawaguti
PRO
9
2.5k
開発生産性大幅アップ!Postman VS Code拡張機能
nagix
2
380
Google Cloud Next '24でブログを10本書いた方法と勉強会を沸かせた方法
yasumuusan
0
290
エンジニアのキャリアをちょっと楽しくする3本の軸/Three Pillars to Make an Engineer's Career More Enjoyable
kwappa
0
2.7k
Google Cloud Next '24 Recap(Cloud Run/k8s)
mokocm
0
200
EMとして2023年度に頑張ったこと / What we did well in FY2023 as a EM
pauli
1
170
Azureの基本的な権限管理の勉強会
yhana
0
430
Meta Quest 3 で動く桜マシマシ WebXR アプリを IBM Cloud Code Engine と Babylon.js で作った話
1ftseabass
PRO
0
120
競技としてのKaggle、役に立つKaggle
yu4u
3
1.3k
AOAI をきっかけに 社内の Azure 管理を見直した話
recruitengineers
PRO
1
290
どうするコスト最適化のトレードオフ
tetsuyaooooo
1
520
オーナーシップを持つ領域を明確にする
konifar
13
3.2k
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
73
5.2k
What the flash - Photography Introduction
edds
64
11k
Happy Clients
brianwarren
92
6.4k
Learning to Love Humans: Emotional Interface Design
aarron
267
39k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
Six Lessons from altMBA
skipperchong
21
3k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
14
1.6k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Building Flexible Design Systems
yeseniaperezcruz
319
37k
Rebuilding a faster, lazier Slack
samanthasiow
73
8.2k
Designing for Performance
lara
601
67k
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?