如何理解Python装饰器?



                    
                    
露西亞嘉利
6584 次浏览 2024-05-27 提问
45

最新回答 (1条回答)

2024-05-27 回答

如上面代码中的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()就是在函数上加个包装

相关问题

页面运行时间: 0.21223306655884 秒