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
nakasho
0
690
PHPでResult型やってみよう
higaki_program
0
190
MCP とマネージド PaaS で実現する大規模 AI アプリケーションの高速開発
nahokoxxx
1
1.4k
低レイヤソフトウェア技術者が YouTuberとして食っていこうとした話
sat
PRO
7
5.8k
OTel 公式ドキュメント翻訳 PJ から始めるコミュニティ活動/Community activities starting with the OTel official document translation project
msksgm
0
220
PdM業務における使い分け
shinshiro
0
580
An introduction to Claude Code SDK
choplin
3
3.3k
スプリントゴール未達症候群に送る処方箋
kakehashi
PRO
1
190
AI エンジニアの立場からみた、AI コーディング時代の開発の品質向上の取り組みと妄想
soh9834
6
250
FAST導入1年間のふりかえり〜現実を直視し、さらなる進化を求めて〜 / Review of the first year of FAST implementation
wooootack
1
120
生成AIによる情報システムへのインパクト
taka_aki
1
110
分散トレーシングによる コネクティッドカーのデータ処理見える化の試み
thatsdone
0
190
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
GitHub's CSS Performance
jonrohan
1031
460k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Designing for Performance
lara
610
69k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
282
13k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
21
1.3k
The Cost Of JavaScript in 2023
addyosmani
51
8.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
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?