-
-
Save shanechin/7d06ca7c2813a85b79a7 to your computer and use it in GitHub Desktop.
This file contains 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
# After spending too much time away from Python on Javascript, I gave this a shot. To my surprise, it worked! | |
# Since Python doesn't bind "self" explicitly in classes, this looks almost exactly like Python classes. | |
# The only visible difference is that you cannot simply access the members using the dot syntax (cat.sayHi). | |
# It must be accessed as a dictionary. | |
def Cat(legs, colorId, name): | |
def sayHi(): | |
print 'Hi, my name is %s. I have %s legs and am %s.' % (this['name'], this['legs'], this['color']) | |
this = { | |
'legs': legs, | |
'color': ['black', 'brown'][colorId], | |
'name': name, | |
'sayHi': sayHi | |
} | |
return this | |
myCat = Cat(4, 0, 'Joey') | |
hisCat = Cat(3, 1, 'Bill') | |
myCat['sayHi']() # Joey | |
hisCat['sayHi']() # Bill |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment