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.
Python Type-Based       Object         V.SJavaScript prototype      陳信屹 (Chen Hsin-YI)    COSCUP 2012, 8/18, Taiwan
Who am I•    陳信屹 a.k.a hychen                   O•••    Python User since 2006    本來是學 Perl, 然後學不會,所以就一路寫 Python 了    Debi...
Outline● Object, Prototype in  JavaScript● Type, Class, Object in Python● Same Part● Difference
Object, Prototype in JavaScript
萬物皆物件Everything is an object                At least in Python...
Class In JavaScript?function MyClass(){   this.name = hello;}MyClass.prototype.say = function(){  console.log(ma ya);}obj ...
New keyword in JavaScript●   Create a new instance of Function Objects    prototype●   Bind this of Function Object to new...
Create a new object based on an        object in JavaScript!function MyClass(){   this.name = hello           constructor}...
Type, Class, Object     in Python
Type → Class → Object 型別產生類別產生物件
Type is an object to  create an ClassClass is an object to  create an object
type: an object tocreate an class objectobject: a base classobject of all classobjects
__new__: create an object__init__ : initial an object
Subclassing a class In Pythonclass MyClass(object):   __metaclass__ = TraceClassCreation                               Def...
Type creates a Class creates an Object-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,)...
Type creates a Class creates an object-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,)...
Type creates a Class creates an object-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,)...
Type creates a Class creates an object-> Enter: type.__new__(<class __main__.TraceClassCreation>,MyClass, (<type object>,)...
Same / Difference
Same Part●   Base Object    ●   Python: object class object    ●   JavaScript: Object object●   Create a new instance of a...
Base Object   ●   Python: object class      ●   JavaScript: Object       object                        objectclass MyClass...
Create a new instance of a base                object●   class.__new__                ●    new keyword                    ...
Constructor is for initial the new                 instance●   class.__init__               ●   constructor of            ...
Difference: Base Object●   base class defines basic     ●   prototype defines basic    attributes/methods of a          at...
Base Object Chain                 function(){}.prototype                                          {}.prototypetype.__base_...
Difference: create new instance●   Can change object    ●   How to change the    creation behavior        behavior of new?...
Difference: constructor●   You can change    ●   Which instance is bound    class.__init__        on this    anytime.     ...
Reference• Book, Professional JavaScript for webdevelopers third edition, Chapter 6, p173,Nicholas C. Zakas• Article, Pyth...
Thanks謝謝聆聽
This slide is released underCreative Commons Attribution-ShareAlike 3.0 Taiwan License
Python new style class v.s JavaScript prototype @COSCUP 2012
Python new style class v.s JavaScript prototype @COSCUP 2012
Upcoming SlideShare
Loading in …5
×

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

5,687 views

Published on

To explain python and javascript object model.

Published in: Technology
  • Be the first to comment

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

×