如何理解Python装饰器

2024-05-12 08:02:21 (31分钟前 更新) 112 8434

最新回答

# 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()就是在函数上加个包装
# 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()就是在函数上加个包装
壮儿象象 2024-05-12

扩展回答

热门问答

装修专题

其他人还看了

页面运行时间: 0.15290188789368 秒