Python 2-7 装饰器

Python装饰器 @decorator ['dekəreɪtə]

Python函数装饰器,就是接受函数作为参数,在不修改函数源代码的基础上,并对函数做一些包装,然后返回增加了包装的函数,即生成了一个新函数。

在编程过程中,我们经常遇到这样的场景:登录校验,权限校验,日志记录等,这些功能代码在各个环节都可能需要,但又十分雷同,通过装饰器来抽象、剥离这部分代码可以很好解决这类场景。

def decorator_func(some_func):
    # define another wrapper function which modifies some_func
    def wrapper_func():
        print("Wrapper function started")   
        some_func()   
        print("Wrapper function ended")    

    return wrapper_func 
    # Wrapper function add something to the passed function and 

猜你喜欢

转载自blog.csdn.net/weixin_43955170/article/details/112916859
2-7