美文网首页
python装饰器的用途

python装饰器的用途

作者: EdisonDong | 来源:发表于2017-02-04 18:01 被阅读0次

    装饰器(decoration)是python里一个非常重要的特性,可以在不改变原有函数代码的情况下扩展函数的功能。装饰器总是以@开头,其用途失少包括一个几个方面:

    日志

    在没有装饰器特性的编程语言里,通常使用显式声明或者“中间件”来实现,对现有的业务逻辑都有不同程度的侵入性。使用装饰器来实现日志功能会非常干净。例子:

    def log(func):
        def wrapper(*args,**kw):
            print("call %s()" % func.__name__)
            print("excute time is:" + time.strftime("%Y-%m-%d %H:%M:%S"),time.localtime())
            return func(*args,**kw)
        return wrapper
    
    @log
    def excute(name):
        print('excute '+name)
    
    excute('start')
    

    执行结果

    call excute()
    excute time is:2017-02-04 17:03:04 time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=17, tm_min=3, tm_sec=4, tm_wday=5, tm_yday=35, tm_isdst=0)
    excute start
    

    可以看到我们并没有改变excute函数的业务逻辑,而是在函数外部实现了日志功能,对代码没有侵入性,耦合性很低。

    验证(或运行时检查)

    在web应用里,表单验证是非常常见的应用,现在我们使用装饰器来检查一系列验证方法的参数。

    def pre_validate(func):
        def wrapper(*args, **kw):
            if not isinstance(args[0], str):
                raise BaseException("first argument need string")
            else:
                return func(*args, **kw)
    
        return wrapper
    @pre_validate
    def validate_string(string):
        print(string)
    
    
    validate_string('required')
    validate_string(1)
    

    运行结果:

    required
    Traceback (most recent call last):
      File "E:/code/python/untitled/untitled.py", line 36, in <module>
        validate_string(1)
      File "E:/code/python/untitled/untitled.py", line 25, in wrapper
        raise BaseException("first argument need string")
    BaseException: first argument need string
    

    创建框架(主要是路由的使用)

    微型框架Flask是绝佳的例子,它的路由写法如下:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello World!'
    
    @app.route('/home')
    def welcome_home():
        return 'welcome home!'
    
    if __name__ == '__main__':
        app.run(port=7777)
    

    当然,在这背后,flask进行了一定的处理,但是用户并没有感知,调用非常简单。

    相关文章

      网友评论

          本文标题:python装饰器的用途

          本文链接:https://www.haomeiwen.com/subject/vitsittx.html