>>> class test(object):
def simplewrap(self, func):
def wrapper(*args, **kwds):
self.print('----start----')
func(args, kwds)
self.print('----end----')
return wrapper
def print(self, string):
print('In function print')
print(string)
print('Finish')
@self.simplewrap
def wraped(self):
print('In function print')
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
class test(object):
File "<pyshell#24>", line 14, in test
@self.simplewrap
NameError: name 'self' is not defined
装饰器用法不能乱用, self代表当前实例对象, 而此处语法糖后面是装饰器的名称