Skip to content

Instantly share code, notes, and snippets.

@nilcolor
Created June 17, 2011 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilcolor/1031338 to your computer and use it in GitHub Desktop.
Save nilcolor/1031338 to your computer and use it in GitHub Desktop.
JavaScript vs Python
//JavaScript
function _A(num){
var o = {};
o.a = num;
o.square = function(){
return o.a * o.a;
};
return o;
}
a = new _A(10);
console.log(a.a); // 10
a.__proto__.b = 20;
console.log(a.b); // 20
a.b = 30
console.log(a.b); // 30
delete a.b;
console.log(a.b); // 20
function _B(){}
b = new _B()
console.log(b.a); // undefined
b.__proto__ = a;
console.log(b.square()); // 100
# Python
class A(object):
def __init__(self, a):
self.a = a
def square(self):
return self.a * self.a
a = A(10)
print(a.a) # 10
A.b = 20
print(a.b) # 20 - 'delegates' to A
a.b = 30
print(a.b) # 30
del a.b
print(a.b) # 20
class B(object):
pass
b = B()
b.__class__ = A
print(b.square()) # 100 - 'delegates' to A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment