-
-
Save knzm/3258853 to your computer and use it in GitHub Desktop.
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 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): | |
original_type = caller.__class__ | |
caller.__class__ = cls | |
v = method(caller, *args, **kwargs) | |
caller.__class = original_type | |
return v | |
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 A.hello.im_func(B("bar")) | |
print "==" | |
print A.tweets("hey") | |
# B.tweets = A.__dict__["tweets"] | |
B.tweets = classmethod(A.tweets.im_func) | |
print B.tweets("bar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment