Skip to content

Instantly share code, notes, and snippets.

@devsli
Last active August 29, 2015 14:01
Show Gist options
  • Save devsli/82e6a6a20d80e5716798 to your computer and use it in GitHub Desktop.
Save devsli/82e6a6a20d80e5716798 to your computer and use it in GitHub Desktop.
Invoke unbound method
class A(object):
def __init__(self):
self.name = 'Andy'
def tell(what):
def inner(self):
print(" -- %s, %s!" % (what, self.name))
return inner
hi = tell("Hello")
class B(A):
def __init__(self):
self.name = 'Billy Bob'
tell = A.tell.__func__
bye = tell("Bye")
class C(object):
def __init__(self):
self.name = 'Charlie'
tell = A.tell.__func__
bye = tell("See ya")
A().hi() # -- Hello, Andy!
B().bye() # -- Bye, Billy Bob!
C().bye() # -- See ya, Charlie!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment