开发者社区 问答 正文

手写一个判断时间的装饰器

手写一个判断时间的装饰器

展开
收起
珍宝珠 2019-11-11 11:41:46 2395 分享 版权
1 条回答
写回答
取消 提交回答
  • import datetime
    
    
    class TimeException(Exception):
        def __init__(self, exception_info):
            super().__init__()
            self.info = exception_info
    
        def __str__(self):
            return self.info
    
    
    def timecheck(func):
        def wrapper(*args, **kwargs):
            if datetime.datetime.now().year == 2019:
                func(*args, **kwargs)
            else:
                raise TimeException("函数已过时")
    
        return wrapper
    
    
    @timecheck
    def test(name):
        print("Hello {}, 2019 Happy".format(name))
    
    
    if __name__ == "__main__":
        test("backbp")
    
    2019-11-11 13:19:52
    赞同 展开评论
问答地址: