import datetime
import functools
# 定义装饰器time
def time(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print('[', datetime.datetime.now(), ']')
return func(*args, **kwargs)
return wrapper
# 定义带有参数的装饰器time_format
def time_format(format):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(datetime.datetime.now().strftime(format))
return func(*args, **kwargs)
return wrapper
return decorator
# 使用time装饰器
@time
def sayHello():
print("Hello world")
# 使用带参数的装饰器
@time_format("%Y/%m/%d %H:%M:%S")
def hello_time():
print('hello time')
if __name__ == "__main__":
sayHello()
sayHello()
hello_time()
网友评论