forked from lymin/python_interview_question
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57.py
More file actions
29 lines (22 loc) · 728 Bytes
/
57.py
File metadata and controls
29 lines (22 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import datetime
# 参考链接:https://foofish.net/python-decorator.html
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("out of time")
return wrapper
@timecheck
# @在此处的用法相当于timecheck(test()),且当运行到代码时,
# test函数已经消失了,再想调用其属性时会报错
def test(name):
print('hello {}'.format(name))
if __name__ == "__main__":
test('world')