What CanMeta ClassDo For You?            hychenPyCon Taiwan 2012, 6/10, Taiwan
Who am I陳信屹 a.k.a hychen                      OPython Developer since 2006本來是學 Perl, 然後學不會,所以就一路寫 Python 了Debian Maintaine...
Python 2New-StyleClass Only
OutlineWhat is metaclass?Example  Singleton  Countable  Class Verification  Logged  Auto Delegation  ...Conclusion
Type → Class → Object  型別產生類別產生物件
Type → Class XXXXX型別產生類別 XXXXX
Objects are instances of classes.       物件是類別的實例 Classes are instance of types.      類別是型別的實例
所有類別繼承 object 類別object class is base class of all classes.       所有類別是 type 的實例    type is metaclass of all classes.
Subclassing a classclass MyClass(object):     name = None     sex = None                              Same>>> MyClass = ty...
Class is an instance of a meta class if..Class has __metaclass__, orClasss ancetor class has__metaclass__type is default m...
subclassing a typeclass MyType(type):    def __new__(mbs, name, bases, _dict):         return type.__new__(mbs, name, base...
MyClass = Mytype()MyType() is MyType.__call__() is …MyClass = type.__new__(MyType....)type.__init__(MyClass....)
應用For Example.....
壹Singleton
Before using metaclass
After using metclass
Only one instance      please use get_instance function        to get the instance      136815764      please use get_inst...
The Magic
貳Countable
Class code
RunI have 0 booksI have 4 books
The Magic
參Class Verification
Class code
>>> from cls_verification import JokeInPTT>>> ERROR: Can not find HaHaPoint in JokeInPTT                同樣的,這場 Talk 也找不到笑點...
The Magic
肆Logged
Class code
Run info:Enter func:get info:Got return value:I am get   method info:Exit func:get
The Magic
Logger function
伍Delegation
Class Code
Run      the data is [1, 2, 3, 4, 5]      girl args
無量大數                      > 10^68Composed ClassAssociationAuto Initial AttributesMore and more..
ConclusionGood Less work, play more Inheritance → composition Metaclass focus on trait of a classBad Too Magic, it is not ...
Q: When should I useMetaClass?A: Never (as long as youreasking this question anyway)              ~ Shal ab h Chat ur v e ...
ReferenceArticle, Python types and objects, ShalabhChaturvediSlide, Meta Class Programming in Python, Juan-Manuel GimenoSl...
If your colleague complain your       code, My friend Thinker Li       suggests a solution....# This is a meta class,# you...
Thanks謝謝聆聽
This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License
What can meta class do for you? @PyCon Taiwan 2012
Upcoming SlideShare
Loading in …5
×

What can meta class do for you? @PyCon Taiwan 2012

7,659 views

Published on

To introduce example of meta class in Python

Published in: Technology, Education
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total views
7,659
On SlideShare
0
From Embeds
0
Number of Embeds
4,619
Actions
Shares
0
Downloads
49
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide
  • I start to use Python since 2006? I choose to use Python is because Perl is too hard to me... And I also maintian 2 python module package 10 年開始在Canonical 工作, 但是主要的工作不是寫python...
  • Pytho 主力語言還是在 2.7, 所以我今天主要討論的範圍是 python 2
  • How to define a metaclass Talk about 5 examples The good/bad of metaclass
  • Python 裡的物件其實分為 Type and Non-Type, Type 跟 Class 都是 Type Type 產生 class , class 產生物件. 所以在metaclass 在 python 裡指的是 type
  • 今天主要 focus 在 type
  • 我們定義class 長這樣 那上面的這部份其實跟下面這部份是等價的 所以你可以在interpreter 中 一步一步去產生一個class
  • New 決定 class 怎麼產生 Init 決定 class 產生後要做什麼動作
  • 這邊描述 class 怎麼產生
  • Class 跟 Type 的關係講完了
  • Singleton Pattern 指的是一個class 只能產生一個instance, 不管他做建立幾個物件,指得都是同一個物件 比如說 Database 通常在程式執行中只有一個實例
  • 有些東西是可以數的 那你可能會希望有個地方可以記一下目前建立幾個物件了 . 如果用 metaclass 的話,你可以讓 class 自己知道自己有幾個 instance.
  • 另一種類似的應用是讓 class 知道繼承它的 class 有幾個 , 這可以幹嗎呢 ? 假設你有個 class 叫 Extension , 其他人要寫 plugin , 只要繼承你的 plugin class, 再做一些客製化就可 因為只要 runtime 時知道有己 class 繼承 plugin class, 你可以利用這個方式知道有幾個 plugin 被 enable.
  • 因為 python 是 weak type , 那你如果想跟strong type 一樣, 可以在instance 建立前就可以check class 是不是有實做所有要求的介面. 你可以用metaclass 達到類似的功能
  • 下一頁說, 何時要用metaclass
  • What can meta class do for you? @PyCon Taiwan 2012

    1. 1. What CanMeta ClassDo For You? hychenPyCon Taiwan 2012, 6/10, Taiwan
    2. 2. Who am I陳信屹 a.k.a hychen OPython Developer since 2006本來是學 Perl, 然後學不會,所以就一路寫 Python 了Debian MaintainerMaintain 2 Python module package MSoftware Engineer in Canonical Ltd.http://hychen.wuweig.orghttp://about.me/hychen
    3. 3. Python 2New-StyleClass Only
    4. 4. OutlineWhat is metaclass?Example Singleton Countable Class Verification Logged Auto Delegation ...Conclusion
    5. 5. Type → Class → Object 型別產生類別產生物件
    6. 6. Type → Class XXXXX型別產生類別 XXXXX
    7. 7. Objects are instances of classes. 物件是類別的實例 Classes are instance of types. 類別是型別的實例
    8. 8. 所有類別繼承 object 類別object class is base class of all classes. 所有類別是 type 的實例 type is metaclass of all classes.
    9. 9. Subclassing a classclass MyClass(object): name = None sex = None Same>>> MyClass = type.__new__(type, MyClass, (), {name:None, sex:None})>>> type.__init__(MyClass, MyClass, (), {name:None, sex:None}>>> MyClass.__class__>>> <type type>>>> MyClass.__bases__>>> (<type object>,)
    10. 10. Class is an instance of a meta class if..Class has __metaclass__, orClasss ancetor class has__metaclass__type is default metaclass
    11. 11. subclassing a typeclass MyType(type): def __new__(mbs, name, bases, _dict): return type.__new__(mbs, name, bases, _dict) def __init__(cls, name, bases, _dict): return type.__init__(cls, name, bases, _dict)
    12. 12. MyClass = Mytype()MyType() is MyType.__call__() is …MyClass = type.__new__(MyType....)type.__init__(MyClass....)
    13. 13. 應用For Example.....
    14. 14. 壹Singleton
    15. 15. Before using metaclass
    16. 16. After using metclass
    17. 17. Only one instance please use get_instance function to get the instance 136815764 please use get_instance function to get the instance 136815764 please use get_instance function to get the instance 136815764
    18. 18. The Magic
    19. 19. 貳Countable
    20. 20. Class code
    21. 21. RunI have 0 booksI have 4 books
    22. 22. The Magic
    23. 23. 參Class Verification
    24. 24. Class code
    25. 25. >>> from cls_verification import JokeInPTT>>> ERROR: Can not find HaHaPoint in JokeInPTT 同樣的,這場 Talk 也找不到笑點 ...
    26. 26. The Magic
    27. 27. 肆Logged
    28. 28. Class code
    29. 29. Run info:Enter func:get info:Got return value:I am get method info:Exit func:get
    30. 30. The Magic
    31. 31. Logger function
    32. 32. 伍Delegation
    33. 33. Class Code
    34. 34. Run the data is [1, 2, 3, 4, 5] girl args
    35. 35. 無量大數 > 10^68Composed ClassAssociationAuto Initial AttributesMore and more..
    36. 36. ConclusionGood Less work, play more Inheritance → composition Metaclass focus on trait of a classBad Too Magic, it is not easy to understand You colleague may complain you
    37. 37. Q: When should I useMetaClass?A: Never (as long as youreasking this question anyway) ~ Shal ab h Chat ur v e d i
    38. 38. ReferenceArticle, Python types and objects, ShalabhChaturvediSlide, Meta Class Programming in Python, Juan-Manuel GimenoSlide, Metaclass In Python, Guy WienerMetaclass programming in Python, IBMDeveloperWorld
    39. 39. If your colleague complain your code, My friend Thinker Li suggests a solution....# This is a meta class,# you are not supposed to read it,# so skip it please.class Singleton(type): …. Just a kidding....
    40. 40. Thanks謝謝聆聽
    41. 41. This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License

    ×