python实现一个判断时间的装饰,并抛出自定义异常

import datetime


class TimeException(Exception):
    def __init__(self, exception_info):
        super().__init__()
        self.info = exception_info

    def __str__(self):
        return self.info


def time_check(func):
    def wrapper(*args, **kwargs):
        if datetime.datetime.now().year == 2020:
            func(*args, **kwargs)
        else:
            raise TimeException("函数已过时")

    return wrapper


@time_check
def test(name):
    print("Hello {}, 2021 Happy".format(name))


if __name__ == "__main__":
    test("backbp")
原文地址:https://www.cnblogs.com/wlike/p/15598642.html