Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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,807 views

Published on

To introduce example of meta class in Python

Published in: Technology, Education

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

×