装修问答

如何理解Python装饰器?

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

推三轮去拉萨 332024-06-09