Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created May 28, 2009 21:40
Show Gist options
  • Save nesquena/119597 to your computer and use it in GitHub Desktop.
Save nesquena/119597 to your computer and use it in GitHub Desktop.
class Demo:
def __init__(self, n):
self.iv = n
def hello(self):
return "Hello, self.iv = %s" % self.iv
k = Demo(99)
m = getattr(k, "hello")
print m() # Hello, self.iv = 99
n = k.hello
print n() # Hello, self.iv = 99
# EVAL
print eval("Demo(99).hello()") # Hello, self.iv = 99It
# EX 2
class Adding:
def add(self, v1, v2):
return v1 + v2
def compute(self, method_object, value1, value2):
return method_object(value1, value2)
calc = Adding()
print calc.compute(getattr(calc, 'add'), 4, 5) # 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment