Created
June 11, 2010 07:22
-
-
Save qingfeng/434163 to your computer and use it in GitHub Desktop.
PyCon
This file contains hidden or 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
from trace import traced | |
def tracing(cls): | |
cdict = cls.__dict__ | |
for n in cdict: | |
if hasattr(cdict[n], "__call__") \ | |
and not n.startswith("__"): | |
cdict[n] = traced(cdict[n]) | |
return cls | |
if __name__ == "__main__": | |
@tracing | |
class MyClass(): | |
def method1(self, a): | |
print("Method 1:", a) | |
def method2(self, a, b=10): | |
return "Method 2: %s" % (a*b) | |
mc = MyClass() | |
mc.method1("Hooray!") | |
print(mc.method2("Easy", b=3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可是有
__call__
的不一定就是方法呀…… 是不是用 descriptor 来做更好一些……