首页 > 装修问答 > 装饰 > 如何理解Python装饰器?

如何理解Python装饰器?

浏览次数:1163|时间:2024-05-06

热门回答

2024-05-15等开到荼蘼
如上面代码中的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()就是在函数上加个包装

53