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

如何理解Python装饰器?

浏览次数:2429|时间:2024-04-28

热门回答

2024-04-27不想在你身后
# hello=inject_check(hello)########################## class Demo(object):# print " d = Demo()d: def my_method(*args);hello": print " return my_method############################ #class Demo(object):# def hello(self): print "before": @inject_check def hello(self);hello",如上面代码中的hello函数加上@inject_check装饰器; method(*args) print "def inject_check(method),等价于将函数hello重新赋值;after".hello()就是在函数上加个包装

205