Python new style class v.s JavaScript prototype @COSCUP 2012

5,307 views
5,192 views

Published on

To explain python and javascript object model.

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

No Downloads
Views
Total views
5,307
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
26
Comments
0
Likes
4
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...
  • 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 new style class v.s JavaScript prototype @COSCUP 2012

    1. 1. Python Type-Based Object V.SJavaScript prototype 陳信屹 (Chen Hsin-YI) COSCUP 2012, 8/18, Taiwan
    2. 2. Who am I• 陳信屹 a.k.a hychen O••• Python User since 2006 本來是學 Perl, 然後學不會,所以就一路寫 Python 了 Debian Maintainer M• Maintain 2 Python module package• Software Engineer in Canonical Ltd.• http://hychen.wuweig.org• http://about.me/hychen
    3. 3. Outline● Object, Prototype in JavaScript● Type, Class, Object in Python● Same Part● Difference
    4. 4. Object, Prototype in JavaScript
    5. 5. 萬物皆物件Everything is an object At least in Python...
    6. 6. Class In JavaScript?function MyClass(){ this.name = hello;}MyClass.prototype.say = function(){ console.log(ma ya);}obj = new MyClass();
    7. 7. New keyword in JavaScript● Create a new instance of Function Objects prototype● Bind this of Function Object to new instance● Execute Function Object function MyClass(){● this is bound on a window } this.name = hello in browser obj = new MyClass(); obj.name // hello MyClass(); this.name // hello
    8. 8. Create a new object based on an object in JavaScript!function MyClass(){ this.name = hello constructor}MyClass.prototype.say = function say(){};obj = Object.create(MyClass.prototype); Base ObjectMyClass.call(obj);
    9. 9. Type, Class, Object in Python
    10. 10. Type → Class → Object 型別產生類別產生物件
    11. 11. Type is an object to create an ClassClass is an object to create an object
    12. 12. type: an object tocreate an class objectobject: a base classobject of all classobjects
    13. 13. __new__: create an object__init__ : initial an object
    14. 14. Subclassing a class In Pythonclass MyClass(object): __metaclass__ = TraceClassCreation Default is type
    15. 15. Type creates a Class creates an Object-> 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>
    16. 16. Type creates a Class creates an object-> 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>
    17. 17. Type creates a Class creates an object-> 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>
    18. 18. Type creates a Class creates an object-> 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>
    19. 19. Same / Difference
    20. 20. Same Part● Base Object ● Python: object class object ● JavaScript: Object object● Create a new instance of a base object ● Python: class.__new__ ● JavaScript: new keyword● Constructor is for initial the new instance ● Python: class.__init__ ● JavaScript: function(){}
    21. 21. Base Object ● Python: object class ● JavaScript: Object object objectclass MyClass(object): function MyClass(){ def __init__(self): } pass # object# <type object> typeof(MyClass.prototype)MyClass.__base__ # undefinedobj = MyClass() typeof(MyClass.prototype.prototype)# <type object>obj.__class__.__base__
    22. 22. Create a new instance of a base object● class.__new__ ● new keyword ● Object.createclass MyClass(object): function MyClass() pass { }obj = MyClass.__new__(MyClass) obj = Object.create(MyClass);
    23. 23. Constructor is for initial the new instance● class.__init__ ● constructor of prototype class MyClass(object): function MyClass(){ this.name = hello; def __init__(self): } self.name = hello // [Function: MyClass] MyClass.prototype.constructor
    24. 24. Difference: Base Object● base class defines basic ● prototype defines basic attributes/methods of a attributes/methods of an class object● class defines basic ● objects prototype is attributes/methods of an changeable instance of class ● Only one base object● classs base class is not changeable● Multiple base class object Python Code JavaScript Code
    25. 25. Base Object Chain function(){}.prototype {}.prototypetype.__base__class.__base__
    26. 26. Difference: create new instance● Can change object ● How to change the creation behavior behavior of new? ● type.__new__ ● type.__init__ ● class.__new__ ● class.__init__ Python Code JavaScript Code
    27. 27. Difference: constructor● You can change ● Which instance is bound class.__init__ on this anytime. ● You can not change the constructor of a object Python Code JavaScript Code
    28. 28. Reference• Book, Professional JavaScript for webdevelopers third edition, Chapter 6, p173,Nicholas C. Zakas• Article, Python types and objects, ShalabhChaturvedi• Slide, Meta Class Programming in Python, Juan-Manuel Gimeno• Slide, Metaclass In Python, Guy Wiener• Metaclass programming in Python, IBMDeveloperWorld
    29. 29. Thanks謝謝聆聽
    30. 30. This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License

    ×