Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created August 4, 2012 06:57
Show Gist options
  • Save podhmo/3255254 to your computer and use it in GitHub Desktop.
Save podhmo/3255254 to your computer and use it in GitHub Desktop.
class A(object):
fmt = "tweets %s"
def hello(self):
return "hello " + self.name
@classmethod
def tweets(cls, mes):
return cls.fmt % mes
def __init__(self, name):
self.name = name
class B(object):
fmt = "called with B: %s"
def __init__(self, name):
self.name = name
def intercept(cls, method):
def doaction(caller, *args, **kwargs):
caller.__class__ = cls
return method.im_func(caller, *args, **kwargs)
return doaction
print A("foo").hello()
try:
print A.hello(B("bar"))
except TypeError, e:
print e
print intercept(A, A.hello)(B("bar"))
print "=="
print A.tweets("hey")
B.tweets = A.__dict__["tweets"]
print B.tweets("bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment