def myzsq(func): def wraper(): print 'begin' func() print 'end---' return wraper class TestMyClass: def __test(self): print 'test-----123123-' @myzsq def run(self): self.__test() t1 = TestMyClass() t1.run()
报错了
Traceback (most recent call last):
File "D:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pycharm\utrunner.py", line 116, in
modules = [loadSource(a[0])]
File "D:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pycharm\utrunner.py", line 40, in loadSource
module = imp.load_source(moduleName, fileName)
File "D:\python-test\testclass.py", line 35, in
t1.run()
TypeError: wraper() takes no arguments (1 given)
self 是参数
得用带参数的装饰器
def decorator(args): def _deco(func): def _func(self): print args print 'gooooo' func(self) print 'fooo' return _func return _deco