python new style class v.s javascript prototype @coscup 2012

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

If you can't read please download the document

Upload: hsinyi-chen

Post on 16-Apr-2017

5.611 views

Category:

Technology


1 download

TRANSCRIPT

Python Type-Based Object
V.S
JavaScript prototype

(Chen Hsin-YI)

COSCUP 2012, 8/18, Taiwan

Who am I

a.k.a hychen

Python User since 2006

Perl, Python

Debian Maintainer

Maintain 2 Python module package

Software Engineer in Canonical Ltd.

http://hychen.wuweig.org

http://about.me/hychen

M

O

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 package10 Canonical , python...

Outline

Object, Prototype in JavaScript

Type, Class, Object in Python

Same Part

Difference

How to define a metaclassTalk about 5 examplesThe good/bad of metaclass

Object, Prototype in JavaScript

Everything is an object

At least in Python...

function MyClass(){this.name = 'hello';}

MyClass.prototype.say = function(){console.log('ma ya');}

obj = new MyClass();

Class In JavaScript?

New keyword in JavaScript

Create a new instance of Function Object's prototype

Bind this of Function Object to new instance

Execute Function Object

this is bound on a window
in browser

function MyClass(){this.name = 'hello'}

obj = new MyClass();

obj.name // hello

MyClass();

this.name // hello

function MyClass(){this.name = 'hello'
}

MyClass.prototype.say = function say(){};
obj = Object.create(MyClass.prototype);

MyClass.call(obj);

Create a new object based on an object in JavaScript!

Base Object

constructor

Type, Class, Object in Python

Type Class Object

Python Type and Non-Type,

Type Class Type

Type class , class .

metaclass python type

Type is an object to create an Class

Class is an object to create an object

type: an object to create an class object

object: a base class object of all class objects

__new__: create an object

__init__ : initial an object

class MyClass(object): __metaclass__ = TraceClassCreation

Subclassing a class In Python

Default is type

-> Enter: type.__new__(, MyClass, (,), {'__module__': '__main__', '__metaclass__': }) Enter: type.__init__(, MyClass, (,), {'__module__': '__main__', '__metaclass__': }) Enter: MyClass.__call__ --> Enter: MyClass.__new__(, , 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__init__(, 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__new__(, , 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__init__(, 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__new__(, , 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__init__(, 1, 2, 3, {'k2': 2, 'k1': 1}) > MyClass(1,2,3 , k1=1, k2=2)

-> Enter: type.__new__(, MyClass, (,), {'__module__': '__main__', '__metaclass__': }) Enter: type.__init__(, MyClass, (,), {'__module__': '__main__', '__metaclass__': }) Enter: MyClass.__call__ --> Enter: MyClass.__new__(, , 1, 2, 3, {'k2': 2, 'k1': 1}) Enter: MyClass.__init__(, 1, 2, 3, {'k2': 2, 'k1': 1})