import time
# 第一件事情调用calculateTime
# 第二件事情把被装饰的函数传递给function
# 第三件事情,调用demo时,等于调用inner()
def calculateTime(function):
print("开始执行了")
def inner(n, *args, **kwargs):
print("args{}".format(args))
print("kwargs{}".format(kwargs))
start = time.time()
result = function(n)
end = time.time()
print("耗时{}".format(end - start))
return result
return inner
@calculateTime
def demo1(n):
total = 0
for i in range(0, n):
total += i
return total
@calculateTime
def demo2(n):
print(n)
total = 0
for i in range(0, 1000000):
total += i
print(demo1(10000, 10000, name="huyong"))
print(demo2)
笔记14-装饰器
最新推荐文章于 2024-09-06 07:22:03 发布