Skip to content

Instantly share code, notes, and snippets.

@rodrigomanhaes
Created October 25, 2010 13:44
Show Gist options
  • Save rodrigomanhaes/644968 to your computer and use it in GitHub Desktop.
Save rodrigomanhaes/644968 to your computer and use it in GitHub Desktop.
Dynamic Python x JavaScript
getattr(drew, "battle_cry")()
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!!!
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
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!!')
def throw_star(self):
print "throwing a star"
import new
drew.throw_star = new.instancemethod(throw_star, drew, Ninja)
drew.throw_star()
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