python类里面使用装饰器

时间:2021-11-13 16:13:54
参考:http://m.blog.csdn.net/blog/wklken/8118942

def on(name, *args_of_on, **kwargs_of_on):
def handle_func(func):
def handle_func_args(self, *args_of_func, **kwargs_of_func):
print "see args_of_on:", name, args_of_on, kwargs_of_on
print "see args_of_func:", self, args_of_func, kwargs_of_func func(self, *args_of_func, **kwargs_of_func)
# if getattr(self, "on_"+name, False):
# getattr(self, "on_"+name)(args)
return handle_func_args
return handle_func class XX(object):
# def on_tick(self, *args):
# print self, *args @on("tick", 123)
def on_data(self, xx):
print "see in on_data:", self, xx a = XX()
print a
a.on_data(55)