Python Object Model@PyHug 07/2012
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Python Object Model@PyHug 07/2012

on

  • 770 views

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

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.

Statistics

Views

Total Views
770
Views on SlideShare
770
Embed Views
0

Actions

Likes
1
Downloads
18
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Presentation Transcript

  • 1. Python Object Model 陳信屹 (Chen Hsin-YI) PyCon HUG 2012, 7/16, Taiwan
  • 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. PythonNew-StyleClass Only
  • 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. 萬物皆物件Everything is an object
  • 6. class MyClass(object): passMyClass: subclass of object classobject: base class of all objects
  • 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. 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. Class is an object to create an object
  • 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. 萬物皆物件 Everything is an object 型別也是物件 Types are objectsisinstance(types.FunctionType, object) is Trueisinstance(type, object) is True
  • 12. type: an class objectto create an classobjectobject: base classobject of all objects
  • 13. 型別與非型別物件Type and Non-type Object
  • 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. Type → Class → Object 型別產生類別產生物件
  • 16. Objects are instances of classes. 物件是類別的實例 Classes are instance of types. 類別是型別的實例
  • 17. 所有類別繼承 object 類別object class is base class of all classes. 所有類別是 type 的實例 type is metaclass of all classes.
  • 18. 物件如何被產生How does an object be created?
  • 19. Subclassing a classclass MyClass(object): __metaclass__ = TraceClassCreation Change default type for tracing the class creation flow
  • 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. 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. 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. 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. 屬性如何被存取How does a attribute be accessed?
  • 25. Quiz of accessing attributes “Tony” , “No Name” or “my name” ?
  • 26. Quiz of accessing attributes “Tony” “my name”
  • 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. 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. type: decide how does class becreatedobject: decide which basicattributes an object should have
  • 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. ReferenceArticle, Python types and objects, ShalabhChaturvediSlide, Meta Class Programming in Python, Juan-Manuel GimenoSlide, Metaclass In Python, Guy WienerMetaclass programming in Python, IBMDeveloperWorld
  • 32. Thanks謝謝聆聽
  • 33. This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License