装饰器:在某个方法执行前后去执行其他新定义的行为
例如:
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ def before_say_hello(): print "before hello" def after_say_hello(): print "after hello" def say_hello_wrapper(func): def wrapper(): print "Before" before_say_hello() func() print "After" after_say_hello() return wrapper @say_hello_wrapper def say_hello(): print "Hello!" if __name__ == "__main__": say_hello()
执行结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello! After after hello Process finished with exit code 0
2.装饰器的执行步骤
(1)首先,装饰器本身也是一个函数,即say_hello_wrapper也是一个函数,也需要在执行前加入到内存
(2)当执行到@say_hello_wrapper时,即将say_hello()替换为say_hello_wrapper()里面的wrapper(), 即为相同的内存地址
(3)当执行到say_hello()时,则会执行say_hello_wrapper()
注意:使用debug模式可知装饰器的执行步骤
3. 带参数函数的装饰器
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ def before_say_hello(): print "before hello" def after_say_hello(): print "after hello" def say_hello_wrapper(func): def wrapper(content): print "Before" before_say_hello() func(content) print "After" after_say_hello() return wrapper @say_hello_wrapper def say_hello(content): print "Hello, {0}".format(content) if __name__ == "__main__": say_hello("World")
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello, World After after hello Process finished with exit code 0
注意:只需将参数加入到wrapper()中即可,因为wrapper()为替换函数
4.带返回值函数的装饰器
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ def before_say_hello(): print "before hello" def after_say_hello(): print "after hello" def say_hello_wrapper(func): def wrapper(content): print "Before" before_say_hello() ret = func(content) print "After" after_say_hello() return ret return wrapper @say_hello_wrapper def say_hello(content): print "Hello, {0}".format(content) return content if __name__ == "__main__": ret = say_hello("World") print ret
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day14/decorator_test.py Before before hello Hello, World After after hello World Process finished with exit code 0