瞭解python的裝飾器特性

Bre-eZe發表於2024-04-09

裝飾器相當於一個裝飾,不修改函式原本內容,只是增添內容

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)()

相關文章