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

2019-06-15  本文已影响0人  dnsir

实例

#! -*- 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属性发生变化的问题)

上一篇下一篇

猜你喜欢

热点阅读