如何理解Python装饰器

2024-06-01 03:50:07 (28分钟前 更新) 306 9758

最新回答

如上面代码中的hello函数加上@inject_check装饰器: def my_method(*args)def inject_check(method); method(*args) print ":# def hello(self): print "hello": @inject_check def hello(self);after"# hello=inject_check(hello)########################## class Demo(object): print " d = Demo()d:# print "hello"before",等价于将函数hello重新赋值; return my_method############################ #class Demo(object).hello()就是在函数上加个包装
如上面代码中的hello函数加上@inject_check装饰器: def my_method(*args)def inject_check(method); method(*args) print ":# def hello(self): print "hello": @inject_check def hello(self);after"# hello=inject_check(hello)########################## class Demo(object): print " d = Demo()d:# print "hello"before",等价于将函数hello重新赋值; return my_method############################ #class Demo(object).hello()就是在函数上加个包装
反叛的路鲁修 2024-06-01

扩展回答

热门问答

装修专题

其他人还看了

页面运行时间: 0.20721888542175 秒