Python Object    Model    陳信屹 (Chen Hsin-YI) PyCon HUG 2012, 7/16, Taiwan
Who am I陳信屹 a.k.a hychen                      OPython User since 2006本來是學 Perl, 然後學不會,所以就一路寫 Python 了Debian MaintainerMain...
PythonNew-StyleClass Only
OutlineEvery thing is an object in PythonType-Object and Non-Type ObjectHow does an object be created?How does a attribute...
萬物皆物件Everything is an object
class MyClass(object):     passMyClass: subclass of object classobject: base class of all objects
Every thing is an objectisinstance(1, object) is Trueisinstance(, object) is Trueisinstance([], object) is Trueisinstance(...
Every thing is an objectisinstance(object, object) is Trueisinstance(str, object) is Trueisinstance(list, object) is Truei...
Class is an object   to create an      object
Type is class of classtype(object) # <type type>type(int)    # <type type>type(str)    # <type type>type(list)   # <type t...
萬物皆物件          Everything is an object                 型別也是物件               Types are objectsisinstance(types.FunctionType...
type: an class objectto create an classobjectobject: base classobject of all objects
型別與非型別物件Type and Non-type Object
What is difference?    If an object is an instance of <type type>,    then it is a type. Otherwise, it is not a type.>>> t...
Type → Class → Object  型別產生類別產生物件
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.
物件如何被產生How does an object be created?
Subclassing a classclass MyClass(object):   __metaclass__ = TraceClassCreation                     Change default type for...
Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>...
Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>...
Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>...
Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>...
屬性如何被存取How does a attribute be accessed?
Quiz of accessing attributes                 “Tony” , “No Name”                  or “my name” ?
Quiz of accessing attributes                   “Tony”                  “my name”
Why?When a attribute be accessedReturn a attribute if a __dict__ of an instance has this attributeIf not found, then conti...
Bound and UnboundInstance attributes is bound, its classs attributesis unbound.obj = MyClass()new_value = obj.attr # obj.a...
type: decide how does class becreatedobject: decide which basicattributes an object should have
Does Python Object model matter?The found mental knowledge when you want tousing meta class Add the code in type.__new__ o...
ReferenceArticle, Python types and objects, ShalabhChaturvediSlide, Meta Class Programming in Python, Juan-Manuel GimenoSl...
Thanks謝謝聆聽
This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License
Upcoming SlideShare
Loading in …5
×

Python Object Model@PyHug 07/2012

827 views

Published on

It is a 25 minutes small talk in Python User Group in 新竹.

Python Object Model is a big topic,
my talk is more about Python type and object.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
20
Comments
0
Likes
2
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
  • Python Object Model@PyHug 07/2012

    1. 1. Python Object Model 陳信屹 (Chen Hsin-YI) PyCon HUG 2012, 7/16, Taiwan
    2. 2. Who am I陳信屹 a.k.a hychen OPython User since 2006本來是學 Perl, 然後學不會,所以就一路寫 Python 了Debian MaintainerMaintain 2 Python module package MSoftware Engineer in Canonical Ltd.http://hychen.wuweig.orghttp://about.me/hychen
    3. 3. PythonNew-StyleClass Only
    4. 4. OutlineEvery thing is an object in PythonType-Object and Non-Type ObjectHow does an object be created?How does a attribute be accessed?Conclusion
    5. 5. 萬物皆物件Everything is an object
    6. 6. class MyClass(object): passMyClass: subclass of object classobject: base class of all objects
    7. 7. Every thing is an objectisinstance(1, object) is Trueisinstance(, object) is Trueisinstance([], object) is Trueisinstance((), object) is Trueisinstance({}, object) is Trueisinstance(lambda e: e, object) is True
    8. 8. Every thing is an objectisinstance(object, object) is Trueisinstance(str, object) is Trueisinstance(list, object) is Trueisinstance(tuple, object) is Trueisinstance(dict, object) is Trueisinstance(types.FunctionType, object) is True
    9. 9. Class is an object to create an object
    10. 10. Type is class of classtype(object) # <type type>type(int) # <type type>type(str) # <type type>type(list) # <type type>type(tuple) # <type type>type(dict) # <type type>type(types.FunctionType, type) # <type type>
    11. 11. 萬物皆物件 Everything is an object 型別也是物件 Types are objectsisinstance(types.FunctionType, object) is Trueisinstance(type, object) is True
    12. 12. type: an class objectto create an classobjectobject: base classobject of all objects
    13. 13. 型別與非型別物件Type and Non-type Object
    14. 14. What is difference? If an object is an instance of <type type>, then it is a type. Otherwise, it is not a type.>>> type(list)<type type>>>> type([])<type object>
    15. 15. Type → Class → Object 型別產生類別產生物件
    16. 16. Objects are instances of classes. 物件是類別的實例 Classes are instance of types. 類別是型別的實例
    17. 17. 所有類別繼承 object 類別object class is base class of all classes. 所有類別是 type 的實例 type is metaclass of all classes.
    18. 18. 物件如何被產生How does an object be created?
    19. 19. Subclassing a classclass MyClass(object): __metaclass__ = TraceClassCreation Change default type for tracing the class creation flow
    20. 20. Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,), {__module__: __main__,__metaclass__: <class __main__.TraceClassCreation>})<- Return: <class __main__.MyClass> which is an instance of<class __main__.TraceClassCreation>-> Enter: type.__init__(<class __main__.MyClass>, MyClass,(<type object>,), {__module__: __main__, __metaclass__:<class __main__.TraceClassCreation>})<- Return: None-> Enter: MyClass.__call__ --> Enter: MyClass.__new__(<class __main__.MyClass>, <class__main__.MyClass>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: <__main__.MyClass object at 0x2512cd0> --> Enter: MyClass.__init__(<__main__.MyClass object at0x2512cd0>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: None<- Return: <__main__.MyClass object at 0x2512cd0> which is aninstance of <class __main__.MyClass>
    21. 21. Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,), {__module__: __main__,__metaclass__: <class __main__.TraceClassCreation>})<- Return: <class __main__.MyClass> which is an instance of<class __main__.TraceClassCreation>-> Enter: type.__init__(<class __main__.MyClass>, MyClass,(<type object>,), {__module__: __main__, __metaclass__:<class __main__.TraceClassCreation>})<- Return: None-> Enter: MyClass.__call__ --> Enter: MyClass.__new__(<class __main__.MyClass>, <class__main__.MyClass>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: <__main__.MyClass object at 0x2512cd0> --> Enter: MyClass.__init__(<__main__.MyClass object at0x2512cd0>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: None<- Return: <__main__.MyClass object at 0x2512cd0> which is aninstance of <class __main__.MyClass>
    22. 22. Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,), {__module__: __main__,__metaclass__: <class __main__.TraceClassCreation>}) >>> MyClass(1,2,3 , k1=1, k2=2)<- Return: <class __main__.MyClass> which is an instance of<class __main__.TraceClassCreation> <__main__.MyClass object at 0x2512cd0>-> Enter: type.__init__(<class __main__.MyClass>, MyClass,(<type object>,), {__module__: __main__, __metaclass__:<class __main__.TraceClassCreation>})<- Return: None-> Enter: MyClass.__call__ --> Enter: MyClass.__new__(<class __main__.MyClass>, <class__main__.MyClass>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: <__main__.MyClass object at 0x2512cd0> --> Enter: MyClass.__init__(<__main__.MyClass object at0x2512cd0>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: None<- Return: <__main__.MyClass object at 0x2512cd0> which is aninstance of <class __main__.MyClass>
    23. 23. Type creates a Class creates an Instance-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,), {__module__: __main__,__metaclass__: <class __main__.TraceClassCreation>})<- Return: <class __main__.MyClass> which is an instance of<class __main__.TraceClassCreation>-> Enter: type.__init__(<class __main__.MyClass>, MyClass,(<type object>,), {__module__: __main__, __metaclass__:<class __main__.TraceClassCreation>})<- Return: None-> Enter: MyClass.__call__ --> Enter: MyClass.__new__(<class __main__.MyClass>, <class__main__.MyClass>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: <__main__.MyClass object at 0x2512cd0> --> Enter: MyClass.__init__(<__main__.MyClass object at0x2512cd0>, 1, 2, 3, {k2: 2, k1: 1}) <-- Return: None<- Return: <__main__.MyClass object at 0x2512cd0> which is aninstance of <class __main__.MyClass>
    24. 24. 屬性如何被存取How does a attribute be accessed?
    25. 25. Quiz of accessing attributes “Tony” , “No Name” or “my name” ?
    26. 26. Quiz of accessing attributes “Tony” “my name”
    27. 27. Why?When a attribute be accessedReturn a attribute if a __dict__ of an instance has this attributeIf not found, then continue search __dict__of base classesIn previous example, the name attributes is always be set to “my name” when __init__ method executed
    28. 28. Bound and UnboundInstance attributes is bound, its classs attributesis unbound.obj = MyClass()new_value = obj.attr # obj.attr.__get__(obj,MyClass)obj.attr = new_value # obj.attr.__set__(obj,new_value)
    29. 29. type: decide how does class becreatedobject: decide which basicattributes an object should have
    30. 30. Does Python Object model matter?The found mental knowledge when you want tousing meta class Add the code in type.__new__ or type.__init__? Which phase will be impact?No surprise when you modify or access aattributesThis concept is not only in Python but also inanother programming language
    31. 31. ReferenceArticle, Python types and objects, ShalabhChaturvediSlide, Meta Class Programming in Python, Juan-Manuel GimenoSlide, Metaclass In Python, Guy WienerMetaclass programming in Python, IBMDeveloperWorld
    32. 32. Thanks謝謝聆聽
    33. 33. This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License

    ×