Python的装饰器(三)

能不能向装饰器中传值呢,答案是肯定的,下面看一个比较综合的栗子,他包含了装饰器的通用格式

import time
from functools import wraps


def log(path):
    def mydecorate(func):
        @wraps(func)
        def inside_func(*args, **kwargs):
            msg = time.ctime() + " " + func.__name__ + " was called \n"
            print(msg, end="")
            with open(path, "a") as f:
                f.write(msg)
            return func(*args, **kwargs)

        return inside_func

    return mydecorate


@log("log.txt")
def show():
    print("this is showshow")


for i in range(3):
    show()
    time.sleep(1)

猜你喜欢

转载自blog.csdn.net/blastblade/article/details/81112825
今日推荐