如何理解Python装饰器

2024-06-03 15:01:19 (43分钟前 更新) 322 9605

最新回答

def inject_check(method): def my_method(*args): print "before" method(*args) print "after" return my_method############################ #class Demo(object):# def hello(self):# pri迹绩管啃攮救归寻害默nt "hello"# hello=inject_check(hello)########################## class Demo(object): @inject_check def hello(self): print "hello" d = Demo()d.hello()就是在函数上加个包装,如上面代码中的hello函数加上@inject_check装饰器,等价于将函数hello重新赋值:hello=inject_check(hello)
def inject_check(method): def my_method(*args): print "before" method(*args) print "after" return my_method############################ #class Demo(object):# def hello(self):# pri迹绩管啃攮救归寻害默nt "hello"# hello=inject_check(hello)########################## class Demo(object): @inject_check def hello(self): print "hello" d = Demo()d.hello()就是在函数上加个包装,如上面代码中的hello函数加上@inject_check装饰器,等价于将函数hello重新赋值:hello=inject_check(hello)
吃出新味来 2024-06-03

扩展回答

热门问答

装修专题

其他人还看了

页面运行时间: 0.63638997077942 秒