美文网首页
Python装饰器6-开始使用装饰器语法

Python装饰器6-开始使用装饰器语法

作者: dnsir | 来源:发表于2019-06-15 11:26 被阅读0次

    实例

    #! -*- coding: utf-8 -*-
    
    """
    装饰器存在的语法问题
    """
    def a_new_decorator(a_func):
    
        def wrapTheFunction():
            print("I am doing some boring work before a_func()")
    
            a_func()
    
            print("im am doing some boring work after a_func()")
    
        return wrapTheFunction
    
    @a_new_decorator
    def a_function_requiring_decoration():
        print("I am the function which need some decoration to remove my foul smell")
    
    a_function_requiring_decoration()
    
    print(a_function_requiring_decoration.__name__)
    
    

    小结

    装饰器语法@就是在运行时实现了函数名重新指向另外一个函数,但是简单使用装饰器语法糖@并没有解决前一篇提到的函数名不变(依然是a_function_requiring_decoration,但是name属性发生变化的问题)

    相关文章

      网友评论

          本文标题:Python装饰器6-开始使用装饰器语法

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