了解python的装饰器特性

装饰器相当于一个装饰,不修改函数原本内容,只是增添内容

def my_decorator(func):
    def warpper():
        print("有函数要执行了")
        func()
        print("有函数执行完毕")

    return warpper

@my_decorator
def say_hello():
    print("hello")


say_hello()

相当于

def my_decorator(func):
    def warpper():
        print("有函数要执行了")
        func()
        print("有函数执行完毕")

    return warpper


def say_hello():
    print("hello")


my_decorator(say_hello)()
posted @ 2024-04-09 23:04  Bre-eZe  阅读(4)  评论(0编辑  收藏  举报