Created
October 25, 2010 13:44
-
-
Save rodrigomanhaes/644968 to your computer and use it in GitHub Desktop.
Dynamic Python x JavaScript
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
getattr(drew, "battle_cry")() |
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
def battle_cry(self): | |
print "%s says zing!!!" % self.name | |
Ninja.battle_cry = battle_cry | |
drew.battle_cry() | |
# Drew says zing!!! | |
adam.battle_cry() | |
# Adam says zing!!! |
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
color_name = 'black' | |
def color(self): | |
print "%s's color is %s" % (self.name, color_name) | |
Ninja.color = color | |
drew.color() | |
# Drew's color is black | |
adam.color() | |
# Adam's color is black |
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
sword_symbol = "*********" | |
def swing(self, sound_effect): | |
print "%s: %s %s" % (self.name, sword_symbol, sound_effect) | |
drew.swing = new.instancemethod(swing, drew, Ninja) | |
drew.swing('slash!!') |
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
def throw_star(self): | |
print "throwing a star" | |
import new | |
drew.throw_star = new.instancemethod(throw_star, drew, Ninja) | |
drew.throw_star() |
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
class Ninja(object): | |
def __init__(self, name): | |
self.name = name | |
drew = Ninja("Drew") | |
adam = Ninja("Adam") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment