Created
June 17, 2011 12:36
-
-
Save nilcolor/1031338 to your computer and use it in GitHub Desktop.
JavaScript vs Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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